-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
189 lines (176 loc) · 4.28 KB
/
scanner.go
File metadata and controls
189 lines (176 loc) · 4.28 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"bufio"
"os"
"strings"
"unicode"
)
// Represents the possible states of the DFA.
type State int
// Represents the possible states of the DFA.
const (
Initial State = iota
Identifier
DotIdentifier
Register
Zero
Decimal
Hexadecimal
Comma
LParen
RParen
LabelDef
Comment
String
)
// String method to convert the current state to a string.
func (s State) String() string {
return [...]string{"Initial", "Identifier", "DotIdentifier", "Register", "Zero", "Decimal", "Hexadecimal", "Comma", "LParen", "RParen", "LabelDef", "Comment", "String"}[s]
}
// Represents a token in the scanner.
type Token struct {
tokenType string
tokenValue string
}
// Represents the scanning Deterministic Finite Automaton(DFA) for the scanner.
type DFA struct {
currentState State
currentToken string
currentString rune
tokens []Token
totalTokens [][]Token
}
// Constructor to initialize memory for the DFA.
func NewDFA() (*DFA, error) {
dfa := &DFA{}
dfa.currentState = Initial
dfa.currentToken = ""
dfa.currentString = 0
dfa.tokens = []Token{}
dfa.totalTokens = [][]Token{}
return dfa, nil
}
// Add a token to the list of tokens.
func (dfa *DFA) AddToken(tokenType string, tokenValue string) {
dfa.tokens = append(dfa.tokens, Token{tokenType, tokenValue})
}
// Store the current state of the DFA.
func (dfa *DFA) Store() {
if dfa.currentState != Initial && dfa.currentState != Comment {
dfa.AddToken(dfa.currentState.String(), dfa.currentToken)
}
dfa.Reset()
}
func (dfa *DFA) StoreLine() {
if len(dfa.tokens) > 0 {
dfa.totalTokens = append(dfa.totalTokens, dfa.tokens)
dfa.tokens = []Token{}
}
}
// Reset the DFA to its initial state.
func (dfa *DFA) Reset() {
dfa.currentState = Initial
dfa.currentToken = ""
dfa.currentString = 0
}
// Transition the DFA to a new state based on the input.
func (dfa *DFA) Transition(input rune) {
// fmt.Printf("State: %s, Rune: '%c'\n", dfa.currentState.String(), input)
switch dfa.currentState {
case Initial:
if input == '.' {
dfa.currentState = DotIdentifier
} else if input == '$' {
dfa.currentToken = string(input)
dfa.currentState = Register
} else if input == '0' {
dfa.currentState = Zero
} else if unicode.IsDigit(input) || input == '-' {
dfa.currentToken = string(input)
dfa.currentState = Decimal
} else if input == ',' {
dfa.AddToken(dfa.currentState.String(), ",")
dfa.Reset()
} else if input == '(' {
dfa.AddToken(dfa.currentState.String(), "(")
dfa.Reset()
} else if input == ')' {
dfa.AddToken(dfa.currentState.String(), ")")
dfa.Reset()
} else if input == '#' || input == ';' {
dfa.Store()
dfa.currentState = Comment
} else if input == '"' || input == '\'' {
dfa.currentString = input
dfa.currentToken = string(input)
dfa.currentState = String
} else if !unicode.IsSpace(input) {
dfa.currentToken = string(input)
dfa.currentState = Identifier
}
case Identifier:
if input == ':' {
dfa.currentState = LabelDef
dfa.Store()
} else if !unicode.IsSpace(input) {
dfa.currentToken += string(input)
} else {
dfa.Store()
}
case DotIdentifier:
if unicode.IsLetter(input) {
dfa.currentToken += string(input)
} else {
dfa.Store()
}
case Register:
if input == ',' {
dfa.Store()
dfa.AddToken(dfa.currentState.String(), ",")
} else if unicode.IsDigit(input) || unicode.IsLetter(input) {
dfa.currentToken += string(input)
} else {
dfa.Store()
}
case Zero:
if input == 'x' {
dfa.currentState = Hexadecimal
} else if unicode.IsDigit(input) {
dfa.currentToken = string(input)
dfa.currentState = Decimal
} else {
dfa.Store()
}
case Decimal:
if unicode.IsDigit(input) {
dfa.currentToken += string(input)
} else {
dfa.Store()
}
case Hexadecimal:
if strings.ContainsAny(string(input), "0123456789abcdefABCDEF") {
dfa.currentToken += string(input)
} else {
dfa.Store()
}
case String:
dfa.currentToken += string(input)
if input == dfa.currentString {
dfa.Store()
}
}
}
func scanFile(file *os.File, dfa *DFA) error {
scanner := bufio.NewScanner(file)
// Scan the file to generate tokens
for scanner.Scan() {
for _, r := range scanner.Text() {
dfa.Transition(r)
// fmt.Print(i, r)
// fmt.Printf("Index: %d, Rune: %c\n", i, r)
}
dfa.Store()
dfa.StoreLine()
}
return nil
}