parser

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2021 License: BSD-1-Clause, BSD-2-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package parser implements a parser for yacc source files.

Note: Rule.Body element's 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 {
	Values []*ActionValue // For backward compatibility.
	Token  *Token
	Token2 *Token
}

Action represents data reduced by production:

Action:
        '{' '}'
Example
fmt.Println(exampleAST(2, "%% a: { }"))
Output:

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

func (*Action) Pos

func (n *Action) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Action) String

func (n *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 {
	Nlist        []*Name // For backward compatibility.
	Value        string
	Case         int
	NameList     *NameList
	ReservedWord *ReservedWord
	Tag          *Tag
	Token        *Token
	Token2       *Token
}

Definition represents data reduced by productions:

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 a %error-verbose"))
Output:

&parser.Definition{
· Token: example.y:1:1: START "%start", Comments: [],
· Token2: example.y:1:8: IDENTIFIER "a", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(4, "%union{int i} %%"))
Output:

&parser.Definition{
· Value: "{int i}",
· Case: 1,
· Token: example.y:1:1: UNION "%union", Comments: [],
}
Example (Case2)
fmt.Println(exampleAST(6, "%{ %} %error-verbose"))
Output:

&parser.Definition{
· Value: " ",
· Case: 2,
· Token: example.y:1:1: LCURL "%{", Comments: [],
· Token2: example.y:1:4: RCURL "%}", Comments: [],
}
Example (Case3)
fmt.Println(exampleAST(7, "%left a %error-verbose"))
Output:

&parser.Definition{
· Nlist: []*parser.Name{ // len 1
· · 0: &parser.Name{
· · · Identifier: "a",
· · · Number: -1,
· · · Token: example.y:1:7: IDENTIFIER "a", Comments: [],
· · },
· },
· Case: 3,
· NameList: &parser.NameList{
· · Name: &parser.Name{ /* recursive/repetitive pointee not shown */ },
· },
· ReservedWord: &parser.ReservedWord{
· · Case: 1,
· · Token: example.y:1:1: LEFT "%left", Comments: [],
· },
}
Example (Case4)
fmt.Println(exampleAST(8, "%left %error-verbose"))
Output:

&parser.Definition{
· Case: 4,
· ReservedWord: &parser.ReservedWord{
· · Case: 1,
· · Token: example.y:1:1: LEFT "%left", Comments: [],
· },
}
Example (Case5)
fmt.Println(exampleAST(9, "%error-verbose %error-verbose"))
Output:

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

func (*Definition) Pos

func (n *Definition) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Definition) String

func (n *Definition) String() string

String implements fmt.Stringer.

type DefinitionList

type DefinitionList struct {
	Definition     *Definition
	DefinitionList *DefinitionList
}

DefinitionList represents data reduced by productions:

DefinitionList:
        /* empty */
|       DefinitionList Definition  // Case 1
Example
fmt.Println(exampleAST(10, "%error-verbose") == (*DefinitionList)(nil))
Output:

true
Example (Case1)
fmt.Println(exampleAST(11, "%error-verbose %error-verbose"))
Output:

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

func (*DefinitionList) Pos

func (n *DefinitionList) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*DefinitionList) String

func (n *DefinitionList) String() string

String implements fmt.Stringer.

type LiteralStringOpt

type LiteralStringOpt struct {
	Token *Token
}

LiteralStringOpt represents data reduced by productions:

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{
· Token: example.y:1:9: STRING_LITERAL "\"b\"", Comments: [],
}

func (*LiteralStringOpt) Pos

func (n *LiteralStringOpt) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*LiteralStringOpt) String

func (n *LiteralStringOpt) String() string

String implements fmt.Stringer.

type Name

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

Name represents data reduced by productions:

Name:
        IDENTIFIER LiteralStringOpt
|       IDENTIFIER NUMBER LiteralStringOpt  // Case 1
Example
fmt.Println(exampleAST(14, "%left a ,"))
Output:

&parser.Name{
· Identifier: "a",
· Number: -1,
· Token: example.y:1:7: IDENTIFIER "a", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(15, "%left a 98 ,"))
Output:

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

func (*Name) Pos

func (n *Name) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Name) String

func (n *Name) String() string

String implements fmt.Stringer.

type NameList

type NameList struct {
	Case     int
	Name     *Name
	NameList *NameList
	Token    *Token
}

NameList represents data reduced by productions:

NameList:
        Name
|       NameList Name      // Case 1
|       NameList ',' Name  // Case 2
Example
fmt.Println(exampleAST(16, "%left a ,"))
Output:

&parser.NameList{
· Name: &parser.Name{
· · Identifier: "a",
· · Number: -1,
· · Token: example.y:1:7: IDENTIFIER "a", Comments: [],
· },
}
Example (Case1)
fmt.Println(exampleAST(17, "%left a b ,"))
Output:

&parser.NameList{
· Name: &parser.Name{
· · Identifier: "a",
· · Number: -1,
· · Token: example.y:1:7: IDENTIFIER "a", Comments: [],
· },
· NameList: &parser.NameList{
· · Case: 1,
· · Name: &parser.Name{
· · · Identifier: "b",
· · · Number: -1,
· · · Token: example.y:1:9: IDENTIFIER "b", Comments: [],
· · },
· },
}
Example (Case2)
fmt.Println(exampleAST(18, "%left a , b ,"))
Output:

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

func (*NameList) Pos

func (n *NameList) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*NameList) String

func (n *NameList) String() string

String implements fmt.Stringer.

type Node

type Node interface {
	Pos() token.Pos
}

Node represents an AST node.

type Precedence

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

Precedence represents data reduced by productions:

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{
· Identifier: "b",
· Case: 1,
· Token: example.y:1:7: PREC "%prec", Comments: [],
· Token2: example.y:1:13: IDENTIFIER "b", Comments: [],
}
Example (Case2)
fmt.Println(exampleAST(21, "%% a: %prec b { }"))
Output:

&parser.Precedence{
· Identifier: "b",
· Action: &parser.Action{
· · Values: []*parser.ActionValue{ // len 1
· · · 0: &parser.ActionValue{
· · · · Pos: 15,
· · · · Src: "{ }",
· · · },
· · },
· · Token: example.y:1:15: '{' "{", Comments: [],
· · Token2: example.y:1:17: '}' "}", Comments: [],
· },
· Case: 2,
· Token: example.y:1:7: PREC "%prec", Comments: [],
· Token2: example.y:1:13: IDENTIFIER "b", Comments: [],
}
Example (Case3)
fmt.Println(exampleAST(22, "%% a: ;"))
Output:

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

func (*Precedence) Pos

func (n *Precedence) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Precedence) String

func (n *Precedence) String() string

String implements fmt.Stringer.

type ReservedWord

type ReservedWord struct {
	Case  int
	Token *Token
}

ReservedWord represents data reduced by productions:

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:1:1: TOKEN "%token", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(24, "%left <"))
Output:

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

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

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

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

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

func (*ReservedWord) Pos

func (n *ReservedWord) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*ReservedWord) String

func (n *ReservedWord) String() string

String implements fmt.Stringer.

type Rule

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

Rule represents data reduced by productions:

Rule:
        C_IDENTIFIER RuleItemList Precedence
|       '|' RuleItemList Precedence           // Case 1
Example
fmt.Println(exampleAST(29, "%% a: b:"))
Output:

&parser.Rule{
· Name: example.y:1:7: C_IDENTIFIER "b", Comments: [],
· Token: example.y:1:7: C_IDENTIFIER "b", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(30, "%% a: |"))
Output:

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

func (*Rule) Pos

func (n *Rule) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Rule) String

func (n *Rule) String() string

String implements fmt.Stringer.

type RuleItemList

type RuleItemList struct {
	Action       *Action
	Case         int
	RuleItemList *RuleItemList
	Token        *Token
}

RuleItemList represents data reduced by productions:

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:1:7: IDENTIFIER "b", Comments: [],
}
Example (Case2)
fmt.Println(exampleAST(33, "%% a: { }"))
Output:

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

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

func (*RuleItemList) Pos

func (n *RuleItemList) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*RuleItemList) String

func (n *RuleItemList) String() string

String implements fmt.Stringer.

type RuleList

type RuleList struct {
	Case         int
	Precedence   *Precedence
	Rule         *Rule
	RuleItemList *RuleItemList
	RuleList     *RuleList
	Token        *Token
}

RuleList represents data reduced by productions:

RuleList:
        C_IDENTIFIER RuleItemList Precedence
|       RuleList Rule                         // Case 1
Example
fmt.Println(exampleAST(35, "%% a:"))
Output:

&parser.RuleList{
· Token: example.y:1:4: C_IDENTIFIER "a", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(36, "%% a: |"))
Output:

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

func (*RuleList) Pos

func (n *RuleList) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*RuleList) String

func (n *RuleList) String() string

String implements fmt.Stringer.

type Specification

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

Specification represents data reduced by production:

Specification:
        DefinitionList "%%" RuleList Tail
Example
fmt.Println(exampleAST(37, "%% a:"))
Output:

&parser.Specification{
· Rules: []*parser.Rule{ // len 1
· · 0: &parser.Rule{
· · · Name: example.y:1:4: C_IDENTIFIER "a", Comments: [],
· · · Token: example.y:1:4: C_IDENTIFIER "a", Comments: [],
· · },
· },
· RuleList: &parser.RuleList{
· · Token: example.y:1:4: C_IDENTIFIER "a", Comments: [],
· },
· Token: example.y:1:1: MARK "%%", 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) Pos

func (n *Specification) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Specification) String

func (n *Specification) String() string

String implements fmt.Stringer.

type Tag

type Tag struct {
	Token  *Token
	Token2 *Token
	Token3 *Token
}

Tag represents data reduced by productions:

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{
· Token: example.y:1:7: '<' "<", Comments: [],
· Token2: example.y:1:9: IDENTIFIER "a", Comments: [],
· Token3: example.y:1:11: '>' ">", Comments: [],
}

func (*Tag) Pos

func (n *Tag) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Tag) String

func (n *Tag) String() string

String implements fmt.Stringer.

type Tail

type Tail struct {
	Value string
	Token *Token
}

Tail represents data reduced by productions:

Tail:
        "%%"
|       /* empty */  // Case 1
Example
fmt.Println(exampleAST(40, "%% a: %%"))
Output:

&parser.Tail{
· Token: example.y:1:7: MARK "%%", Comments: [],
}
Example (Case1)
fmt.Println(exampleAST(41, "%% a:") == (*Tail)(nil))
Output:

true

func (*Tail) Pos

func (n *Tail) Pos() token.Pos

Pos reports the position of the first component of n or zero if it's empty.

func (*Tail) String

func (n *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