strparse

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2023 License: MIT Imports: 3 Imported by: 5

README

strparse

build-img pkg-img reportcard-img version-img

Package strparse provides convenience wrappers around go/parser for simple expression, statement and declaretion parsing from string.

Installation

Go version 1.16+

go get github.com/go-toolsmith/strparse

Example

package main

import (
	"github.com/go-toolsmith/astequal"
	"github.com/go-toolsmith/strparse"
)

func main() {
	// Comparing AST strings for equallity (note different spacing):
	x := strparse.Expr(`1 + f(v[0].X)`)
	y := strparse.Expr(` 1+f( v[0].X ) `)
	fmt.Println(astequal.Expr(x, y)) // => true
}

License

MIT License.

Documentation

Overview

Package strparse provides convenience wrappers around `go/parser` for simple expression, statement and declaration parsing from string.

Can be used to construct AST nodes using source syntax.

Example
package main

import (
	"fmt"

	"github.com/go-toolsmith/astequal"
	"github.com/go-toolsmith/strparse"
)

func main() {
	// Comparing AST strings for equallity (note different spacing):
	x := strparse.Expr(`1 + f(v[0].X)`)
	y := strparse.Expr(` 1+f( v[0].X ) `)
	fmt.Println(astequal.Expr(x, y)) // => true

}
Output:

true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// BadExpr is returned as a parse result for malformed expressions.
	// Should be treated as constant or readonly variable.
	BadExpr = &ast.BadExpr{}

	// BadStmt is returned as a parse result for malformed statmenents.
	// Should be treated as constant or readonly variable.
	BadStmt = &ast.BadStmt{}

	// BadDecl is returned as a parse result for malformed declarations.
	// Should be treated as constant or readonly variable.
	BadDecl = &ast.BadDecl{}
)

Functions

func Decl

func Decl(s string) ast.Decl

Decl parses single declaration node from s. In case of parse error, BadDecl is returned.

func Expr

func Expr(s string) ast.Expr

Expr parses single expression node from s. In case of parse error, BadExpr is returned.

func Stmt

func Stmt(s string) ast.Stmt

Stmt parses single statement node from s. In case of parse error, BadStmt is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL