-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.js
114 lines (100 loc) · 2.74 KB
/
lexer.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
const { createToken, Lexer } = require('chevrotain')
// the vocabulary will be exported and used in the Parser definition.
const tokenVocabulary = {}
// createToken is used to create a TokenType
// The Lexer's output will contain an array of token Objects created by metadata
const Identifier = createToken({
name: 'Identifier',
pattern: /\*|[a-zA-Z]\w*/,
})
// We specify the "longer_alt" property to resolve keywords vs identifiers ambiguity.
// See: https://github.com/SAP/chevrotain/blob/master/examples/lexer/keywords_vs_identifiers/keywords_vs_identifiers.js
const Select = createToken({
name: 'Select',
pattern: /SELECT/i,
longer_alt: Identifier,
})
const From = createToken({
name: 'From',
pattern: /FROM/i,
longer_alt: Identifier,
})
const Where = createToken({
name: 'Where',
pattern: /WHERE/i,
longer_alt: Identifier,
})
const OrderBy = createToken({
name: 'OrderBy',
pattern: /ORDER BY/i,
longer_alt: Identifier,
})
const Limit = createToken({
name: 'Limit',
pattern: /LIMIT/i,
longer_alt: Identifier,
})
const Comma = createToken({ name: 'Comma', pattern: /,/ })
const Integer = createToken({ name: 'Integer', pattern: /0|[1-9]\d*/ })
const GreaterThanEqual = createToken({
name: 'GreaterThanEqual',
pattern: />=/,
})
const GreaterThan = createToken({
name: 'GreaterThan',
pattern: />/,
})
const LessThanEqual = createToken({ name: 'LessThanEqual', pattern: /<=/ })
const LessThan = createToken({
name: 'LessThan',
pattern: /</,
})
const Asc = createToken({ name: 'Asc', pattern: /ASC/, longer_alt: Identifier })
const Desc = createToken({
name: 'Desc',
pattern: /DESC/,
longer_alt: Identifier,
})
const Equal = createToken({ name: 'Equal', pattern: /=/ })
const NotEqual = createToken({ name: 'NotEqual', pattern: /<>/ })
const WhiteSpace = createToken({
name: 'WhiteSpace',
pattern: /\s+/,
group: Lexer.SKIPPED,
})
let allTokens = [
WhiteSpace,
// "keywords" appear before the Identifier
Select,
From,
Where,
OrderBy,
Limit,
Comma,
Asc,
Desc,
// The Identifier must appear after the keywords because all keywords are valid identifiers.
Identifier,
Integer,
Equal,
NotEqual,
GreaterThanEqual,
GreaterThan,
LessThanEqual,
LessThan,
]
const SelectLexer = new Lexer(allTokens)
allTokens.forEach(tokenType => {
tokenVocabulary[tokenType.name] = tokenType
})
module.exports = {
tokenVocabulary: tokenVocabulary,
lex: function(inputText) {
const lexingResult = SelectLexer.tokenize(inputText)
if (lexingResult.errors.length > 0) {
throw Error('Lexing Errors detected.\n' + lexingResult.errors[0].message)
}
return lexingResult
},
}