Documentation ¶
Index ¶
- func IsEOFError(err error) bool
- func IsEOFInsteadOfNewlineError(err error) bool
- func ParseExpr(x string) (ast.Expression, error)
- func ParseExprFrom(fset *gotoken.FileSet, filename string, src interface{}, mode Mode) (ast.Expression, error)
- func ParseFile(fset *gotoken.FileSet, filename string, src interface{}, mode Mode) (*ast.Program, error)
- type Errors
- type Mode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsEOFError ¶
IsEOFError returns true if err represents an unexpectedTokenError with its actual token type set to token.EOF.
It returns false for any other error.
func IsEOFInsteadOfNewlineError ¶
IsEOFInsteadOfNewlineError returns true if err represents an unexpectedTokenError with its actual token type set to token.EOF and if its expected token types includes token.NEWLINE.
It returns false for any other error.
func ParseExpr ¶
func ParseExpr(x string) (ast.Expression, error)
ParseExpr is a convenience function for obtaining the AST of an expression x. The position information recorded in the AST is undefined. The filename used in error messages is the empty string.
Example ¶
package main import ( "fmt" "github.com/goruby/goruby/parser" ) func main() { src := `def bar() puts "Hello world" end` expr, err := parser.ParseExpr(src) if err != nil { fmt.Println(err) return } fmt.Printf("%T\n", expr) }
Output: *ast.FunctionLiteral
func ParseExprFrom ¶
func ParseExprFrom(fset *gotoken.FileSet, filename string, src interface{}, mode Mode) (ast.Expression, error)
ParseExprFrom is a convenience function for parsing an expression. The arguments have the same meaning as for ParseFile, but the source must be a valid Go (type or value) expression. Specifically, fset must not be nil.
func ParseFile ¶
func ParseFile(fset *gotoken.FileSet, filename string, src interface{}, mode Mode) (*ast.Program, error)
ParseFile parses the source code of a single Ruby source file and returns the corresponding ast.Program node. The source code may be provided via the filename of the source file, or via the src parameter.
If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.
The mode parameter controls the amount of source text parsed and other optional parser functionality. Position information is recorded in the file set fset, which must not be nil.
If the source couldn't be read or the source was read but syntax errors were found, the returned AST is nil and the error indicates the specific failure.
Example ¶
package main import ( "fmt" "go/token" "github.com/goruby/goruby/ast" "github.com/goruby/goruby/parser" ) func main() { fset := token.NewFileSet() // positions are relative to fset src := `LANG = "Ruby" module Foo def bar() puts "Hello world" end end` f, err := parser.ParseFile(fset, "", src, parser.AllErrors) if err != nil { fmt.Println(err) return } // Print the statements from the programs AST for _, s := range f.Statements { if exp, ok := s.(*ast.ExpressionStatement); ok { fmt.Printf("%T\n", exp.Expression) } else { fmt.Printf("%T\n", s) } } }
Output: *ast.Assignment *ast.ModuleExpression
Types ¶
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors represents a group of errors and its context
Errors implements the error interface to be used as an error in the code.