-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserbase.go
92 lines (74 loc) · 2.88 KB
/
parserbase.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package parser
import (
"strings"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
// JavaScriptParserBase implementation.
type JavaScriptParserBase struct {
*antlr.BaseParser
}
// Short for p.prev(str string)
func (p *JavaScriptParserBase) p(str string) bool {
return p.prev(str)
}
// Whether the previous token value equals to str.
func (p *JavaScriptParserBase) prev(str string) bool {
return p.GetTokenStream().LT(-1).GetText() == str
}
// Short for p.next(str string)
func (p *JavaScriptParserBase) n(str string) bool {
return p.next(str)
}
// Whether the next token value equals to str.
func (p *JavaScriptParserBase) next(str string) bool {
return p.GetTokenStream().LT(1).GetText() == str
}
func (p *JavaScriptParserBase) notLineTerminator() bool {
return !p.here(JavaScriptParserLineTerminator)
}
func (p *JavaScriptParserBase) notOpenBraceAndNotFunction() bool {
nextTokenType := p.GetTokenStream().LT(1).GetTokenType()
return nextTokenType != JavaScriptParserOpenBrace && nextTokenType != JavaScriptParserFunction
}
func (p *JavaScriptParserBase) closeBrace() bool {
return p.GetTokenStream().LT(1).GetTokenType() == JavaScriptParserCloseBrace
}
// Returns true if on the current index of the parser's
// token stream a token of the given type exists on the
// Hidden channel.
func (p *JavaScriptParserBase) here(_type int) bool {
// Get the token ahead of the current index.
possibleIndexEosToken := p.GetCurrentToken().GetTokenIndex() - 1
ahead := p.GetTokenStream().Get(possibleIndexEosToken)
// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return ahead.GetChannel() == antlr.LexerHidden && ahead.GetTokenType() == _type
}
// Returns true if on the current index of the parser's
// token stream a token exists on the Hidden channel which
// either is a line terminator, or is a multi line comment that
// contains a line terminator.
func (p *JavaScriptParserBase) lineTerminatorAhead() bool {
// Get the token ahead of the current index.
possibleIndexEosToken := p.GetCurrentToken().GetTokenIndex() - 1
ahead := p.GetTokenStream().Get(possibleIndexEosToken)
if ahead.GetChannel() != antlr.LexerHidden {
// We're only interested in tokens on the HIDDEN channel.
return true
}
if ahead.GetTokenType() == JavaScriptParserLineTerminator {
// There is definitely a line terminator ahead.
return true
}
if ahead.GetTokenType() == JavaScriptParserWhiteSpaces {
// Get the token ahead of the current whitespaces.
possibleIndexEosToken = p.GetCurrentToken().GetTokenIndex() - 2
ahead = p.GetTokenStream().Get(possibleIndexEosToken)
}
// Get the token's text and type.
text := ahead.GetText()
_type := ahead.GetTokenType()
// Check if the token is, or contains a line terminator.
return (_type == JavaScriptParserMultiLineComment && (strings.Contains(text, "\r") || strings.Contains(text, "\n"))) ||
(_type == JavaScriptParserLineTerminator)
}