|
@@ -1,69 +1,59 @@
|
1
|
|
-const tokenTypes = {
|
2
|
|
- OPAREN: '(',
|
3
|
|
- CPAREN: ')',
|
4
|
|
- KEYWORD: 'keyword',
|
5
|
|
- QUOTE: '"',
|
6
|
|
- LITERAL: 'literal',
|
7
|
|
- ATTRIBUTE: 'attribute',
|
8
|
|
- EOF: 'eof',
|
9
|
|
-}
|
|
1
|
+const TokenStream = require('./tokenStream')
|
|
2
|
+const tokenTypes = require('./tokenTypes')
|
10
|
3
|
|
11
|
|
-const scan = source => {
|
12
|
|
- let pos = 0
|
13
|
|
- let line = 1
|
14
|
|
- let tokens = []
|
|
4
|
+module.exports = class Lexer {
|
|
5
|
+ scan(source) {
|
|
6
|
+ let pos = 0
|
|
7
|
+ let line = 1
|
|
8
|
+ let tokens = new TokenStream()
|
15
|
9
|
|
16
|
|
- while (pos < source.length) {
|
17
|
|
- if (source[pos].match(/\(/)) {
|
18
|
|
- tokens.push({
|
19
|
|
- type: tokenTypes.OPAREN,
|
20
|
|
- line: line,
|
21
|
|
- })
|
22
|
|
- pos++
|
23
|
|
- } else if (source[pos].match(/\)/)) {
|
24
|
|
- tokens.push({
|
25
|
|
- type: tokenTypes.CPAREN,
|
26
|
|
- line: line,
|
27
|
|
- })
|
28
|
|
- pos++
|
29
|
|
- } else if (source[pos].match(/['"]/)) {
|
30
|
|
- tokens.push({
|
31
|
|
- type: tokenTypes.QUOTE,
|
32
|
|
- line: line,
|
33
|
|
- })
|
34
|
|
- pos++
|
35
|
|
- } else if (source[pos].match(/:/)) {
|
36
|
|
- let value = /:([^()'"\s]+)/.exec(source)[1].trim()
|
37
|
|
- tokens.push({
|
38
|
|
- type: tokenTypes.ATTRIBUTE,
|
39
|
|
- line: line,
|
40
|
|
- value: value,
|
41
|
|
- })
|
42
|
|
- pos += value.length + 1 // the +1 is to account for the colon
|
43
|
|
- } else if (source[pos].match(/\n/)) {
|
44
|
|
- line++
|
45
|
|
- pos++
|
46
|
|
- } else if (source[pos].match(/\s/)) {
|
47
|
|
- pos++
|
48
|
|
- } else {
|
49
|
|
- let value = /[^()"':]+/.exec(source.slice(pos))[0]
|
50
|
|
- tokens.push({
|
51
|
|
- type: tokenTypes.LITERAL,
|
52
|
|
- line: line,
|
53
|
|
- value: value.trim(),
|
54
|
|
- })
|
55
|
|
- pos += value.length
|
|
10
|
+ while (pos < source.length) {
|
|
11
|
+ if (source[pos].match(/\(/)) {
|
|
12
|
+ tokens.push({
|
|
13
|
+ type: tokenTypes.OPAREN,
|
|
14
|
+ line: line,
|
|
15
|
+ })
|
|
16
|
+ pos++
|
|
17
|
+ } else if (source[pos].match(/\)/)) {
|
|
18
|
+ tokens.push({
|
|
19
|
+ type: tokenTypes.CPAREN,
|
|
20
|
+ line: line,
|
|
21
|
+ })
|
|
22
|
+ pos++
|
|
23
|
+ } else if (source[pos].match(/['"]/)) {
|
|
24
|
+ tokens.push({
|
|
25
|
+ type: tokenTypes.QUOTE,
|
|
26
|
+ line: line,
|
|
27
|
+ })
|
|
28
|
+ pos++
|
|
29
|
+ } else if (source[pos].match(/:/)) {
|
|
30
|
+ let value = /:([^()'"\s]+)/.exec(source)[1].trim()
|
|
31
|
+ tokens.push({
|
|
32
|
+ type: tokenTypes.ATTRIBUTE,
|
|
33
|
+ line: line,
|
|
34
|
+ value: value,
|
|
35
|
+ })
|
|
36
|
+ pos += value.length + 1 // the +1 is to account for the colon
|
|
37
|
+ } else if (source[pos].match(/\n/)) {
|
|
38
|
+ line++
|
|
39
|
+ pos++
|
|
40
|
+ } else if (source[pos].match(/\s/)) {
|
|
41
|
+ pos++
|
|
42
|
+ } else {
|
|
43
|
+ let value = /[^()"':]+/.exec(source.slice(pos))[0]
|
|
44
|
+ tokens.push({
|
|
45
|
+ type: tokenTypes.LITERAL,
|
|
46
|
+ line: line,
|
|
47
|
+ value: value.trim(),
|
|
48
|
+ })
|
|
49
|
+ pos += value.length
|
|
50
|
+ }
|
56
|
51
|
}
|
57
|
|
- }
|
58
|
52
|
|
59
|
|
- tokens.push({
|
60
|
|
- type: tokenTypes.EOF,
|
61
|
|
- line: line,
|
62
|
|
- })
|
63
|
|
- return tokens
|
64
|
|
-}
|
65
|
|
-
|
66
|
|
-module.exports = {
|
67
|
|
- scan: scan,
|
68
|
|
- tokenTypes: tokenTypes,
|
|
53
|
+ tokens.push({
|
|
54
|
+ type: tokenTypes.EOF,
|
|
55
|
+ line: line,
|
|
56
|
+ })
|
|
57
|
+ return tokens
|
|
58
|
+ }
|
69
|
59
|
}
|