parser

package
v0.0.0-...-2bc18d8 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2017 License: BSD-2-Clause, BSD-3-Clause, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package parser implements a parser for yacc source files.

Note: Rule.Body elements type

int		Eg. 65 represents literal 'A'

string		Eg. "Start" represents rule component Start

*Action		Mid rule action or rule semantic action

Index

Examples

Constants

View Source
const (
	// ActionValueGo is used for a Go code fragment
	ActionValueGo = iota
	// ActionValueDlrDlr is used for $$.
	ActionValueDlrDlr
	// ActionValueDlrTagDlr is used for $<tag>$.
	ActionValueDlrTagDlr
	// ActionValueDlrNum is used for $num.
	ActionValueDlrNum
	// ActionValueDlrTagNum is used for $<tag>num.
	ActionValueDlrTagNum
)
View Source
const (
	COMMENT        = 57346
	C_IDENTIFIER   = 57347
	ERROR_VERBOSE  = 57348
	IDENTIFIER     = 57349
	LCURL          = 57350
	LEFT           = 57351
	MARK           = 57352
	NONASSOC       = 57353
	NUMBER         = 57354
	PREC           = 57355
	PRECEDENCE     = 57356
	RCURL          = 57357
	RIGHT          = 57358
	START          = 57359
	STRING_LITERAL = 57360
	TOKEN          = 57361
	TYPE           = 57362
	UNION          = 57363
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Token  *Token
	Token2 *Token
	Pos    token.Pos
	Values []*ActionValue // For backward compatibility.
}

Action represents data reduced by production(s):

Action:
	'{' '}'
Example
fmt.Println(exampleAST(2, `

%%

a:

	{
	}

`))
Output:

&parser.Action{
· Token: example.y:7:2: '{' "{", Comments: [],
· Token2: example.y:8:2: '}' "}", Comments: [],
· Pos: 12,
· Values: []*parser.ActionValue{ // len 1
· · 0: &parser.ActionValue{
· · · Pos: 12,
· · · Src: "{\n\t}",
· · },
· },
}

func (*Action) String

func (a *Action) String() string

String implements fmt.Stringer.

type ActionValue

type ActionValue struct {
	Num  int       // The number in $num.
	Pos  token.Pos // Position of the start of the ActionValue.
	Src  string    // Source for this value.
	Tag  string    // The tag in $<tag>$ or $<tag>num.
	Type int       // One of ActionValue{Go,DlrDlr,DlrTagDlr,DlrNum,DlrTagNum} constants.
}

ActionValue is an item of Action.Value

type Definition

type Definition struct {
	Case         int // 0-5
	NameList     *NameList
	ReservedWord *ReservedWord
	Tag          *Tag
	Token        *Token
	Token2       *Token
	Pos          token.Pos
	Value        string
	Nlist        []*Name // For backward compatibility.
}

Definition represents data reduced by production(s):

Definition:
	START IDENTIFIER
|	UNION                      // Case 1
|	LCURL RCURL                // Case 2
|	ReservedWord Tag NameList  // Case 3
|	ReservedWord Tag           // Case 4
|	ERROR_VERBOSE              // Case 5
Example
fmt.Println(exampleAST(3, `
%start source

%%
`))
Output:

&parser.Definition{
· Token: example.y:2:1: START "%start", Comments: [],
· Token2: example.y:2:8: IDENTIFIER "source", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(4, `
%union{
        foo bar
}

%%
`))
Output:

&parser.Definition{
· Case: 1,
· Token: example.y:2:1: UNION "%union", Comments: [],
· Pos: 8,
· Value: "{\n        foo bar\n}",
}
Example (Case2)
fmt.Println(exampleAST(6, `

%{

%} %error-verbose

`))
Output:

&parser.Definition{
· Case: 2,
· Token: example.y:3:1: LCURL "%{", Comments: [],
· Token2: example.y:5:1: RCURL "%}", Comments: [],
· Pos: 5,
· Value: "\n\n",
}
Example (Case3)
fmt.Println(exampleAST(7, `
%token ARROW "->"
	IDENT
%%
`))
Output:

&parser.Definition{
· Case: 3,
· NameList: &parser.NameList{
· · Name: &parser.Name{
· · · LiteralStringOpt: &parser.LiteralStringOpt{
· · · · Case: 1,
· · · · Token: example.y:2:14: STRING_LITERAL "\"->\"", Comments: [],
· · · },
· · · Token: example.y:2:8: IDENTIFIER "ARROW", Comments: [],
· · · Identifier: "ARROW",
· · · Number: -1,
· · },
· · NameList: &parser.NameList{
· · · Case: 1,
· · · Name: &parser.Name{
· · · · Token: example.y:3:2: IDENTIFIER "IDENT", Comments: [],
· · · · Identifier: "IDENT",
· · · · Number: -1,
· · · },
· · },
· },
· ReservedWord: &parser.ReservedWord{
· · Token: example.y:2:1: TOKEN "%token", Comments: [],
· },
· Nlist: []*parser.Name{ // len 2
· · 0: &parser.Name{ /* recursive/repetitive pointee not shown */ },
· · 1: &parser.Name{ /* recursive/repetitive pointee not shown */ },
· },
}
Example (Case4)
fmt.Println(exampleAST(8, `
%token <abc>
%%
`))
Output:

&parser.Definition{
· Case: 4,
· ReservedWord: &parser.ReservedWord{
· · Token: example.y:2:1: TOKEN "%token", Comments: [],
· },
· Tag: &parser.Tag{
· · Case: 1,
· · Token: example.y:2:8: '<' "<", Comments: [],
· · Token2: example.y:2:9: IDENTIFIER "abc", Comments: [],
· · Token3: example.y:2:12: '>' ">", Comments: [],
· },
}
Example (Case5)
fmt.Println(exampleAST(9, `

%error-verbose %error-verbose

`))
Output:

&parser.Definition{
· Case: 5,
· Token: example.y:3:1: ERROR_VERBOSE "%error-verbose", Comments: [],
}

func (*Definition) String

func (d *Definition) String() string

String implements fmt.Stringer.

type DefinitionList

type DefinitionList struct {
	Case           int // 0-1
	Definition     *Definition
	DefinitionList *DefinitionList
}

DefinitionList represents data reduced by production(s):

DefinitionList:
	/* empty */
|	DefinitionList Definition  // Case 1
Example
fmt.Println(exampleAST(10, `

%error-verbose

`) == (*DefinitionList)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(11, `
%left '+' '-'
%left '*' '/'
%%
`))
Output:

&parser.DefinitionList{
· Case: 1,
· Definition: &parser.Definition{
· · Case: 3,
· · NameList: &parser.NameList{
· · · Name: &parser.Name{
· · · · Token: example.y:2:7: IDENTIFIER "'+'", Comments: [],
· · · · Identifier: 43,
· · · · Number: -1,
· · · },
· · · NameList: &parser.NameList{
· · · · Case: 1,
· · · · Name: &parser.Name{
· · · · · Token: example.y:2:11: IDENTIFIER "'-'", Comments: [],
· · · · · Identifier: 45,
· · · · · Number: -1,
· · · · },
· · · },
· · },
· · ReservedWord: &parser.ReservedWord{
· · · Case: 1,
· · · Token: example.y:2:1: LEFT "%left", Comments: [],
· · },
· · Nlist: []*parser.Name{ // len 2
· · · 0: &parser.Name{ /* recursive/repetitive pointee not shown */ },
· · · 1: &parser.Name{ /* recursive/repetitive pointee not shown */ },
· · },
· },
}

func (*DefinitionList) String

func (d *DefinitionList) String() string

String implements fmt.Stringer.

type LiteralStringOpt

type LiteralStringOpt struct {
	Case  int // 0-1
	Token *Token
}

LiteralStringOpt represents data reduced by production(s):

LiteralStringOpt:
	/* empty */
|	STRING_LITERAL  // Case 1
Example
fmt.Println(exampleAST(12, `

%left
	a ,

`) == (*LiteralStringOpt)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(13, `

%left
	a "@b" ,

`))
Output:

&parser.LiteralStringOpt{
· Case: 1,
· Token: example.y:4:4: STRING_LITERAL "\"@b\"", Comments: [],
}

func (*LiteralStringOpt) String

func (l *LiteralStringOpt) String() string

String implements fmt.Stringer.

type Name

type Name struct {
	Case             int // 0-1
	LiteralStringOpt *LiteralStringOpt
	Token            *Token
	Token2           *Token
	Identifier       interface{} // For backward compatibility.
	Number           int         // For backward compatibility.
}

Name represents data reduced by production(s):

Name:
	IDENTIFIER LiteralStringOpt
|	IDENTIFIER NUMBER LiteralStringOpt  // Case 1
Example
fmt.Println(exampleAST(14, `

%left
	a ,

`))
Output:

&parser.Name{
· Token: example.y:4:2: IDENTIFIER "a", Comments: [],
· Identifier: "a",
· Number: -1,
}
Example (Case1)
fmt.Println(exampleAST(15, `

%left
	a 1 ,

`))
Output:

&parser.Name{
· Case: 1,
· Token: example.y:4:2: IDENTIFIER "a", Comments: [],
· Token2: example.y:4:4: NUMBER "1", Comments: [],
· Identifier: "a",
· Number: 1,
}

func (*Name) String

func (n *Name) String() string

String implements fmt.Stringer.

type NameList

type NameList struct {
	Case     int // 0-2
	Name     *Name
	NameList *NameList
	Token    *Token
}

NameList represents data reduced by production(s):

NameList:
	Name
|	NameList Name      // Case 1
|	NameList ',' Name  // Case 2
Example
fmt.Println(exampleAST(16, `

%left
	a ,

`))
Output:

&parser.NameList{
· Name: &parser.Name{
· · Token: example.y:4:2: IDENTIFIER "a", Comments: [],
· · Identifier: "a",
· · Number: -1,
· },
}
Example (Case1)
fmt.Println(exampleAST(17, `

%left
	a
	b ,

`))
Output:

&parser.NameList{
· Name: &parser.Name{
· · Token: example.y:4:2: IDENTIFIER "a", Comments: [],
· · Identifier: "a",
· · Number: -1,
· },
· NameList: &parser.NameList{
· · Case: 1,
· · Name: &parser.Name{
· · · Token: example.y:5:2: IDENTIFIER "b", Comments: [],
· · · Identifier: "b",
· · · Number: -1,
· · },
· },
}
Example (Case2)
fmt.Println(exampleAST(18, `

%left
	a ,
	b ,

`))
Output:

&parser.NameList{
· Name: &parser.Name{
· · Token: example.y:4:2: IDENTIFIER "a", Comments: [],
· · Identifier: "a",
· · Number: -1,
· },
· NameList: &parser.NameList{
· · Case: 2,
· · Name: &parser.Name{
· · · Token: example.y:5:2: IDENTIFIER "b", Comments: [],
· · · Identifier: "b",
· · · Number: -1,
· · },
· · Token: example.y:4:4: ',' ",", Comments: [],
· },
}

func (*NameList) String

func (n *NameList) String() string

String implements fmt.Stringer.

type Precedence

type Precedence struct {
	Action     *Action
	Case       int // 0-3
	Precedence *Precedence
	Token      *Token
	Token2     *Token
	Identifier interface{} // Name string or literal int.
}

Precedence represents data reduced by production(s):

Precedence:
	/* empty */
|	PREC IDENTIFIER         // Case 1
|	PREC IDENTIFIER Action  // Case 2
|	Precedence ';'          // Case 3
Example
fmt.Println(exampleAST(19, `

%%

a:
|

`) == (*Precedence)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(20, `

%%

a:
%prec
	b

`))
Output:

&parser.Precedence{
· Case: 1,
· Token: example.y:6:1: PREC "%prec", Comments: [],
· Token2: example.y:7:2: IDENTIFIER "b", Comments: [],
· Identifier: "b",
}
Example (Case2)
fmt.Println(exampleAST(21, `

%%

a:
%prec
	b
	{
	}

`))
Output:

&parser.Precedence{
· Action: &parser.Action{
· · Token: example.y:8:2: '{' "{", Comments: [],
· · Token2: example.y:9:2: '}' "}", Comments: [],
· · Pos: 20,
· · Values: []*parser.ActionValue{ // len 1
· · · 0: &parser.ActionValue{
· · · · Pos: 20,
· · · · Src: "{\n\t}",
· · · },
· · },
· },
· Case: 2,
· Token: example.y:6:1: PREC "%prec", Comments: [],
· Token2: example.y:7:2: IDENTIFIER "b", Comments: [],
· Identifier: "b",
}
Example (Case3)
fmt.Println(exampleAST(22, `

%%

a:
;

`))
Output:

&parser.Precedence{
· Case: 3,
· Token: example.y:6:1: ';' ";", Comments: [],
}

func (*Precedence) String

func (p *Precedence) String() string

String implements fmt.Stringer.

type ReservedWord

type ReservedWord struct {
	Case  int // 0-5
	Token *Token
}

ReservedWord represents data reduced by production(s):

ReservedWord:
	TOKEN
|	LEFT        // Case 1
|	RIGHT       // Case 2
|	NONASSOC    // Case 3
|	TYPE        // Case 4
|	PRECEDENCE  // Case 5
Example
fmt.Println(exampleAST(23, `

%token <

`))
Output:

&parser.ReservedWord{
· Token: example.y:3:1: TOKEN "%token", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(24, `

%left <

`))
Output:

&parser.ReservedWord{
· Case: 1,
· Token: example.y:3:1: LEFT "%left", Comments: [],
}
Example (Case2)
fmt.Println(exampleAST(25, `

%right <

`))
Output:

&parser.ReservedWord{
· Case: 2,
· Token: example.y:3:1: RIGHT "%right", Comments: [],
}
Example (Case3)
fmt.Println(exampleAST(26, `

%nonassoc <

`))
Output:

&parser.ReservedWord{
· Case: 3,
· Token: example.y:3:1: NONASSOC "%nonassoc", Comments: [],
}
Example (Case4)
fmt.Println(exampleAST(27, `

%type <

`))
Output:

&parser.ReservedWord{
· Case: 4,
· Token: example.y:3:1: TYPE "%type", Comments: [],
}
Example (Case5)
fmt.Println(exampleAST(28, `

%precedence <

`))
Output:

&parser.ReservedWord{
· Case: 5,
· Token: example.y:3:1: PRECEDENCE "%precedence", Comments: [],
}

func (*ReservedWord) String

func (r *ReservedWord) String() string

String implements fmt.Stringer.

type Rule

type Rule struct {
	Case         int // 0-1
	Precedence   *Precedence
	RuleItemList *RuleItemList
	Token        *Token
	Name         *Token
	Body         []interface{} // For backward compatibility.
}

Rule represents data reduced by production(s):

Rule:
	C_IDENTIFIER RuleItemList Precedence
|	'|' RuleItemList Precedence  // Case 1
Example
fmt.Println(exampleAST(29, `
%%a:b:{c=$1}{d}%%
`))
Output:

&parser.Rule{
· RuleItemList: &parser.RuleItemList{
· · Action: &parser.Action{
· · · Token: example.y:2:7: '{' "{", Comments: [],
· · · Token2: example.y:2:12: '}' "}", Comments: [],
· · · Pos: 14,
· · · Values: []*parser.ActionValue{ // len 3
· · · · 0: &parser.ActionValue{
· · · · · Pos: 8,
· · · · · Src: "{c=",
· · · · },
· · · · 1: &parser.ActionValue{
· · · · · Num: 1,
· · · · · Pos: 11,
· · · · · Src: "$1",
· · · · · Type: 3,
· · · · },
· · · · 2: &parser.ActionValue{
· · · · · Pos: 13,
· · · · · Src: "}",
· · · · },
· · · },
· · },
· · Case: 2,
· · RuleItemList: &parser.RuleItemList{
· · · Action: &parser.Action{
· · · · Token: example.y:2:13: '{' "{", Comments: [],
· · · · Token2: example.y:2:15: '}' "}", Comments: [],
· · · · Pos: 14,
· · · · Values: []*parser.ActionValue{ // len 1
· · · · · 0: &parser.ActionValue{
· · · · · · Pos: 14,
· · · · · · Src: "{d}",
· · · · · },
· · · · },
· · · },
· · · Case: 2,
· · },
· },
· Token: example.y:2:5: C_IDENTIFIER "b", Comments: [],
· Name: example.y:2:5: C_IDENTIFIER "b", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(30, `

%%

a:
|

`))
Output:

&parser.Rule{
· Case: 1,
· Token: example.y:6:1: '|' "|", Comments: [],
· Name: example.y:5:1: C_IDENTIFIER "a", Comments: [],
}

func (*Rule) String

func (r *Rule) String() string

String implements fmt.Stringer.

type RuleItemList

type RuleItemList struct {
	Action       *Action
	Case         int // 0-3
	RuleItemList *RuleItemList
	Token        *Token
}

RuleItemList represents data reduced by production(s):

RuleItemList:
	/* empty */
|	RuleItemList IDENTIFIER      // Case 1
|	RuleItemList Action          // Case 2
|	RuleItemList STRING_LITERAL  // Case 3
Example
fmt.Println(exampleAST(31, `

%%

a:

`) == (*RuleItemList)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(32, `

%%

a:

	b

`))
Output:

&parser.RuleItemList{
· Case: 1,
· Token: example.y:7:2: IDENTIFIER "b", Comments: [],
}
Example (Case2)
fmt.Println(exampleAST(33, `

%%

a:

	{
	}

`))
Output:

&parser.RuleItemList{
· Action: &parser.Action{
· · Token: example.y:7:2: '{' "{", Comments: [],
· · Token2: example.y:8:2: '}' "}", Comments: [],
· · Pos: 12,
· · Values: []*parser.ActionValue{ // len 1
· · · 0: &parser.ActionValue{
· · · · Pos: 12,
· · · · Src: "{\n\t}",
· · · },
· · },
· },
· Case: 2,
}
Example (Case3)
fmt.Println(exampleAST(34, `

%%

a:
"@b"

`))
Output:

&parser.RuleItemList{
· Case: 3,
· Token: example.y:6:1: STRING_LITERAL "\"@b\"", Comments: [],
}

func (*RuleItemList) String

func (r *RuleItemList) String() string

String implements fmt.Stringer.

type RuleList

type RuleList struct {
	Case         int // 0-1
	Precedence   *Precedence
	Rule         *Rule
	RuleItemList *RuleItemList
	RuleList     *RuleList
	Token        *Token
}

RuleList represents data reduced by production(s):

RuleList:
	C_IDENTIFIER RuleItemList Precedence
|	RuleList Rule  // Case 1
Example
fmt.Println(exampleAST(35, `
%%a:{b}{c}%%
`))
Output:

&parser.RuleList{
· RuleItemList: &parser.RuleItemList{
· · Action: &parser.Action{
· · · Token: example.y:2:5: '{' "{", Comments: [],
· · · Token2: example.y:2:7: '}' "}", Comments: [],
· · · Pos: 9,
· · · Values: []*parser.ActionValue{ // len 1
· · · · 0: &parser.ActionValue{
· · · · · Pos: 6,
· · · · · Src: "{b}",
· · · · },
· · · },
· · },
· · Case: 2,
· · RuleItemList: &parser.RuleItemList{
· · · Action: &parser.Action{
· · · · Token: example.y:2:8: '{' "{", Comments: [],
· · · · Token2: example.y:2:10: '}' "}", Comments: [],
· · · · Pos: 9,
· · · · Values: []*parser.ActionValue{ // len 1
· · · · · 0: &parser.ActionValue{
· · · · · · Pos: 9,
· · · · · · Src: "{c}",
· · · · · },
· · · · },
· · · },
· · · Case: 2,
· · },
· },
· Token: example.y:2:3: C_IDENTIFIER "a", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(36, `

%%

a:
|

`))
Output:

&parser.RuleList{
· RuleList: &parser.RuleList{
· · Case: 1,
· · Rule: &parser.Rule{
· · · Case: 1,
· · · Token: example.y:6:1: '|' "|", Comments: [],
· · · Name: example.y:5:1: C_IDENTIFIER "a", Comments: [],
· · },
· },
· Token: example.y:5:1: C_IDENTIFIER "a", Comments: [],
}

func (*RuleList) String

func (r *RuleList) String() string

String implements fmt.Stringer.

type Specification

type Specification struct {
	DefinitionList *DefinitionList
	RuleList       *RuleList
	Tail           *Tail
	Token          *Token
	Defs           []*Definition // For backward compatibility.
	Rules          []*Rule       // For backward compatibility.
}

Specification represents data reduced by production(s):

Specification:
	DefinitionList MARK RuleList Tail
Example
fmt.Println(exampleAST(37, `

%%

a:

`))
Output:

&parser.Specification{
· RuleList: &parser.RuleList{
· · Token: example.y:5:1: C_IDENTIFIER "a", Comments: [],
· },
· Token: example.y:3:1: MARK "%%", Comments: [],
· Rules: []*parser.Rule{ // len 1
· · 0: &parser.Rule{
· · · Token: example.y:5:1: C_IDENTIFIER "a", Comments: [],
· · · Name: example.y:5:1: C_IDENTIFIER "a", Comments: [],
· · },
· },
}

func Parse

func Parse(fset *token.FileSet, fname string, src []byte) (s *Specification, err error)

Parse parses src as a single yacc source file fname and returns the corresponding Specification. If the source couldn't be read, the returned Specification is nil and the error indicates all of the specific failures.

func (*Specification) String

func (s *Specification) String() string

String implements fmt.Stringer.

type Tag

type Tag struct {
	Case   int // 0-1
	Token  *Token
	Token2 *Token
	Token3 *Token
}

Tag represents data reduced by production(s):

Tag:
	/* empty */
|	'<' IDENTIFIER '>'  // Case 1
Example
fmt.Println(exampleAST(38, `

%left %error-verbose

`) == (*Tag)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(39, `

%left <
	a > %error-verbose

`))
Output:

&parser.Tag{
· Case: 1,
· Token: example.y:3:7: '<' "<", Comments: [],
· Token2: example.y:4:2: IDENTIFIER "a", Comments: [],
· Token3: example.y:4:4: '>' ">", Comments: [],
}

func (*Tag) String

func (t *Tag) String() string

String implements fmt.Stringer.

type Tail

type Tail struct {
	Case  int // 0-1
	Token *Token
	Value string
}

Tail represents data reduced by production(s):

Tail:
	MARK
|	/* empty */  // Case 1
Example
fmt.Println(exampleAST(40, `

%%

a:


%%

`))
Output:

&parser.Tail{
· Token: example.y:8:1: MARK "%%", Comments: [],
· Value: "\n\n",
}
Example (Case1)
fmt.Println(exampleAST(41, `

%%

a:

`) == (*Tail)(nil))
Output:

true

func (*Tail) String

func (t *Tail) String() string

String implements fmt.Stringer.

type Token

type Token struct {
	Comments []string
	Val      string
	File     *token.File
	lex.Char
}

Token captures a lexem with position, value and comments, if any.

func (*Token) Pos

func (t *Token) Pos() token.Pos

Pos retruns the token.Pos for t.

func (*Token) Position

func (t *Token) Position() token.Position

Position returns the token.Position for t

func (*Token) String

func (t *Token) String() string

Strings implements fmt.Stringer.

Jump to

Keyboard shortcuts

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