parser

package
v0.0.0-...-9834360 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEOFError

func IsEOFError(err error) bool

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

func IsEOFInsteadOfNewlineError(err error) bool

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.

func NewErrors

func NewErrors(context string, errors ...error) *Errors

NewErrors returns a composite Error object wrapping multiple errors into one.

func (*Errors) Error

func (e *Errors) Error() string

Error returns all error messages divided by newlines and prepended with the error context.

type Mode

type Mode uint

A Mode value is a set of flags (or 0). They control the amount of source code parsed and other optional parser functionality.

const (
	ParseComments Mode = 1 << iota // parse comments and add them to AST
	Trace                          // print a trace of parsed productions
	AllErrors                      // report all errors (not just the first 10 on different lines)
)

parser modes

Jump to

Keyboard shortcuts

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