-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.go
76 lines (56 loc) · 1.87 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package parser
import "github.com/iNamik/go_lexer"
import "github.com/iNamik/go_container/queue"
// StateFn represents the state of the parser as a function that returns the next state.
type StateFn func(Parser) StateFn
// Marker stores the state of the parser to allow rewinding
type Marker struct {
sequence int
pos int
}
// Parser helps you process lexer tokens
type Parser interface {
// Line returns the line number of the next token (aka PeekTotenType(0).Line() )
Line() int
// Column returns the column number of the next token (aka PeekTokenType(0).column() )
Column() int
// PeekTokenType allows you to look ahead at tokens without consuming them
PeekTokenType(int) lexer.TokenType
// PeekToken allows you to look ahead at tokens without consuming them
PeekToken(int) *lexer.Token
// NextToken consumes and returns the next token
NextToken() *lexer.Token
// SkipToken consumes the next token without returning it
SkipToken()
// SkipTokens consumes the next n tokens without returning them
SkipTokens(int)
// BackupToken un-consumes the last token
BackupToken()
// BackupTokens un-consumes the last n tokens
BackupTokens(int)
// ClearTokens clears all consumed tokens
ClearTokens()
// Emit emits an object, consuming matched tokens
Emit(interface{})
EOF() bool
// Next retrieves the next emitted item
Next() interface{}
// Marker returns a marker that you can use to reset the parser state later
Marker() *Marker
// Reset resets the lexer state to the specified marker
Reset(*Marker)
}
// New returns a new Parser object
func New(startState StateFn, lex lexer.Lexer, channelCap int) Parser {
p := &parser{
lex: lex,
tokens: queue.New(4), // 4 is just a nice number that seems appropriate
pos: 0,
sequence: 0,
eofToken: nil,
eof: false,
state: startState,
chn: make(chan interface{}, channelCap),
}
return p
}