MCPcopy Create free account
hub / github.com/crossoverJie/gscript / Parse

Function Parse

syntax/parse.go:35–62  ·  view source on GitHub ↗

Parse syntax parsing. Program -> IntDeclaration | ExpressionStatement | AssignmentStatement IntDeclaration -> 'int' Id ( = Additive) '\n' ExpressionStatement -> Additive '\n' Additive -> Multiplicative ( (+ | -) Multiplicative)* Multiplicative -> Primary ((* | /) Primary)* Primary -> IntLiteral | Id

(script string)

Source from the content-addressed store, hash-verified

33// Primary -> IntLiteral | Id | Additive
34// AssignmentStatement -> Identifier = Additive
35func Parse(script string) (*ASTNode, error) {
36 tokenTypes := token.Tokenize(script)
37 reader := NewTokenReader(tokenTypes)
38
39 root := NewASTNode(Program, App)
40 var err error
41 for reader.Peek() != nil {
42 child, _ := IntDeclare(reader)
43
44 if child == nil {
45 child, err = ExpressionStatement(reader)
46 }
47
48 if child == nil {
49 child, err = AssignmentStatement(reader)
50 }
51
52 if child != nil {
53 root.AddChild(child)
54 } else {
55 return nil, errors.New("syntax err: Invalid statement")
56 }
57 }
58
59 //cal.PrintASTNode(root, "")
60
61 return root, err
62}
63
64// ExpressionStatement -> 1+2*3 '\n'
65func ExpressionStatement(reader *TokenReader) (*ASTNode, error) {

Callers

nothing calls this directly

Calls 8

PeekMethod · 0.95
AddChildMethod · 0.95
TokenizeFunction · 0.92
NewTokenReaderFunction · 0.85
NewASTNodeFunction · 0.85
IntDeclareFunction · 0.85
ExpressionStatementFunction · 0.85
AssignmentStatementFunction · 0.85

Tested by

no test coverage detected