-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (124 loc) · 3.39 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export class Tokenizer {
constructor(userInput, grammar) {
this.position = 0
this.grammar = grammar
this.grammarArray = grammar.getGrammar()
this.userInput = userInput.trim()
this.restOfString = ''
this.maximalMunch = 0
this.match
this.matches = []
this.foundMatch
this.error = 'END'
this.resetValues()
// this.grammar.runRegexsAgainstToken(this.userInput)
this.runRegexsAgainstToken(this.userInput,this.grammarArray)
}
resetValues() {
this.restOfString = ''
this.position = 0
this.matches = []
}
setString(newUserInput) {
this.resetValues()
this.userInput = newUserInput.trim()
this.runRegexsAgainstToken(this.userInput,this.regexArr)
}
runRegexsAgainstToken (word, grammarArray) {
if(this.userInput === '') {
this.match = {'value':'END','tokenType':'END','regex':'No regex'}
} else {
this.maximalMunch = 0
this.foundMatch = false
grammarArray.forEach(reg => {
let match = word.match(reg.regex)
this.checkCorrectMatch(match,reg)
})
this.checkMatchError(this.foundMatch)
this.setMatch(word)
}
}
checkCorrectMatch(match,reg) {
if(this.isMatchAndNotNull(match) && this.checkMaximalMunch(match[0].length,reg)) {
this.setMatchStatusAndValue(match,reg)
}
}
checkMatchError(match) {
if(!match) {
throw new TypeError('Lexical error')
}
}
setMatch(word) {
this.restOfString = word.replace(this.match.getTokenValue(),'').trim()
this.matches.push(this.match)
}
setMatchStatusAndValue = (match,reg) => {
this.foundMatch = true
this.match = new Tokentype(match[0], reg)
}
isMatchAndNotNull = match => {
if(match || match != null) {
return true
}
}
checkMaximalMunch = (match,reg) => {
if(match > this.maximalMunch) {
this.maximalMunch = match
return true
}
}
next = _ => {
this.position++
if(this.position > this.matches.length) {
this.position = this.matches.length
}
if(this.restOfString.length > 0) {
if(this.position <= this.matches.length || !this.matches[this.position]) {
try {
this.runRegexsAgainstToken(this.restOfString,this.grammarArray)
} catch (e) {
throw new TypeError('Lexical error')
}
}
}
}
previous = _ => {
this.position--
this.position = this.position < -1 ? -1 : this.position
}
getCurrentToken = _ => this.position < 0 || this.position >= this.matches.length ? {'value':this.error,'tokenType':this.error} : this.matches[this.position]
getAllMatchedTokens = _ => this.matches
getGrammar = _ => this.grammar
getUserInput = _ => this.userInput
}
export class Grammar {
constructor(regex) {
this.grammarList = regex ?? new Array
}
setGrammar = regexArray => {
this.grammarList = regexArray
}
addGrammar = grammar => {
this.grammarList.push(grammar)
}
getGrammar = _ => this.grammarList
}
export class Tokentype {
constructor(word,regex) {
this.token = {
'value':word,
'tokenType': regex.type,
'sentenceEnding': regex.sentenceEnding,
'regex': regex.regex
}
}
getTokenValue() {
return this.token.value
}
getTokenType() {
return this.token.tokenType
}
getTokenEnding() {
return this.token.sentenceEnding
}
}