/
erlango_types_parser.go
383 lines (290 loc) · 12.4 KB
/
erlango_types_parser.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright (c) 2023, Balazs Nyiro
All rights reserved.
This source code (all file in this repo) is licensed
under the Apache-2 style license found in the
LICENSE file in the root directory of this source tree.
Version 0.2, second rewrite
*/
package old2
import (
"fmt"
"sort"
"strings"
)
type ErlTokens map[int] ErlToken // list AND map, same time. the token's first char position is the key,
// so later it is easy to add new token, and by nature it is a list, too
// and in the tests it is easy to check (if it is not a map in tests it is difficult
// to check the result. if it is a map, the token positions can be checked directly
func (tokens ErlTokens) keysListOfPositions() []int {
// thank you and respect for the faster implementation than append:
// https://stackoverflow.com/questions/21362950/getting-a-slice-of-keys-from-a-map
positionKeys := make([]int, len(tokens))
i := 0
for k := range tokens {
positionKeys[i] = k
i++
}
sort.Ints(positionKeys)
return positionKeys
}
// ############# PARSER ELEMS #############################
func ErlToken_empty_obj(tokenType string, tokenId int) ErlToken { // in program plan
return ErlToken{ TokenType: tokenType, TokenId: tokenId, SourceCodeChars: Chars{}, TokenIsDetectedAsPartOfExpression: false}
}
const tokenType_TextBlockQuotedSingle = "tokenTextBlockQuotedSingle"
const tokenType_Comment = "tokenComment"
const tokenType_TextBlockQuotedDouble = "tokenTextBlockQuotedDouble"
const tokenType_AbcFullWith_Underscore_At_numbers = "tokenAbcFullWith_Underscore_At_numbers"
const tokenType_OtherPunctuation = "tokenOtherPunctuation"
const tokenType_WhiteSpace = "tokenWhiteSpace"
const tokenType_CharLiteral = "tokenCharLiteral"
const tokenType_DigitsOnlyUnderscore_Block = "token_digitBlock"
const tokenType_DigitAndAlphabet_Block__numberDetectionStepInitial = "tokenDigitAndAlphabet_Block__numberDetectionStepInitial"
const tokenType_PlaceholderOnly_DontHaveMeaning = "tokenPlaceholderOnlyDontHaveMeaning "
// use minimal set of types. Don't overcomplicate the parser.
type ErlToken struct {
// don't create a pointer to the prev/next Token.
// it is a dangerous error source. Use only a positionId in a collector of ErlTokens
TokenType string
/* possible token types:
*/
TokenId int
DebugStringRepresentation string // the string representation can be asked with a function, but in the debugger it is easier if it is stored in an attribute
// one char can have a meaning alone, for example: (
// but sometime more than one few char can form a token, for example: '->' which has his own meaning, but represented by 2 chars
SourceCodeChars Chars
TokenIsDetectedAsPartOfExpression bool
}
func (token ErlToken) charFirstRuneVal() rune {
return token.SourceCodeChars[0].Value
}
func (token ErlToken) charFirstRuneValIsSmallCapsAtomStarter() bool {
return strings.Contains(abcEngLower, string(token.charFirstRuneVal()))
}
func (token ErlToken) charFirstRuneValIsVariableStarter() bool {
return strings.Contains("_" + abcEngUpper, string(token.charFirstRuneVal()))
}
func (token ErlToken) charAllInPassedCharacterSet(characterSetAccepted string) bool {
return string_all_chars_in_acceptedCharacterSet(token.stringRepresentation(), characterSetAccepted)
}
func (token ErlToken) stringRepresentationInPassedStringSet(stringSliceAccepted []string) bool {
return string_in_acceptedStrings(token.stringRepresentation(), stringSliceAccepted)
}
func (token ErlToken) typeIsEmpty() bool { // in program plan
return token.TokenType == ""
}
func (token ErlToken) charPositionFirstLast() (int, int) {
charPosFirst := -1 // in a source code, 0 is the smallest position, so -1 means: no real position
charPosLast := -1
if len(token.SourceCodeChars) > 0 {
charPosFirst = token.SourceCodeChars[0].PositionInFile
charPosLast = token.SourceCodeChars[len(token.SourceCodeChars)-1].PositionInFile
}
return charPosFirst, charPosLast
}
func (token ErlToken) charPosFirst() int {
charPosFirst, _ := token.charPositionFirstLast()
return charPosFirst
}
func (token ErlToken) charPosLast() int {
_, charPosLast := token.charPositionFirstLast()
return charPosLast
}
func (token ErlToken) stringRepresentation() string {
runes := []rune{}
for _, charNow := range(token.SourceCodeChars) {
runes = append(runes, charNow.Value)
}
return string(runes)
}
func (token ErlToken) stringRepresentation_escapedSourceForTests() string {
runes := []rune{}
for _, charNow := range(token.SourceCodeChars) {
if charNow.Value =='"' {
runes = append(runes, '\\')
runes = append(runes, '"')
continue
}
if charNow.Value =='\\' {
runes = append(runes, '\\')
runes = append(runes, '\\')
continue
}
if charNow.Value =='\n' {
runes = append(runes, '\\')
runes = append(runes, 'n')
continue
}
if charNow.Value =='\t' {
runes = append(runes, '\\')
runes = append(runes, 't')
continue
}
runes = append(runes, charNow.Value)
}
return string(runes)
}
type Chars []Char
func (chars Chars) print_with_tokens(tokens ErlTokens) {
/*
to check the tokens, one character wide token signs are used.
so the long %%%% means that where you see %, that is a comment
token type: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
source orig: % "double \"quoted\" comment, with a single quoted 'atom'"
positionInFile: 01234567890123456789012345678901234567890123456789012345678
a string example - here the strings and comments are detected only:
??????????????"""""""""""""""""""""""""""""""""""""""""""""""??????????????????????%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
example(1) -> "case 1 \\\" complex string \" with \n newline"; % comment in 'example' function
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
% comments
" strings
a alphabet
v variables
8 numbers
*/
tokenElems := []rune{}
charElems := []rune{}
idElems := []rune{}
tokenFlags := map[string]rune{
"tokenTextBlockQuotedDouble": '"',
"tokenComment": '%',
"tokenTextBlockQuotedSingle": '\'',
"tokenAbcFullWith_Underscore_At_numbers": 'C', // abc lower, upper plus atom builder extra chars
// to avoid mixing it with atoms, C abC is the flag.
// and it stands for character, too ;-)
// it can be an atom, if the first char is uppercase,
// but in first token detection step, only alphabet is checked,
// which has capital letters, too
"tokenOtherPunctuation": ':',
"tokenWhiteSpace": 'w',
"tokenCharLiteral": 'L',
}
for _, char := range chars {
idRune := rune(str_from_int(char.PositionInLine % 10)[0])
idElems = append(idElems, idRune)
if char.Value == '\n' {
charElems = append(charElems, ' ')
} else {
charElems = append(charElems, char.Value)
}
tokenFlag := '?'
// tokens are stored by their position, not by their id.
// if a token is detected for the char position, save the last position too, so we can print the whole token range
// the token ID is not the best id to find it for chars, because tokens are stored by a position in file, not by id.
if char.TokenDetected {
tokenDetected, tokenInTheMap := tokens[char.TokenFirstCharPositionInFile]
if tokenInTheMap {
value, flagDetected := tokenFlags[tokenDetected.TokenType]
if flagDetected {
tokenFlag = value
} else {
fmt.Println("tokenType", tokenDetected.TokenType, "is not in tokenFlags")
}
}
}
tokenElems = append(tokenElems, tokenFlag)
if char.Value == '\n' {
fmt.Println()
fmt.Println()
runes_print(tokenElems)
runes_print(charElems)
runes_print(idElems)
tokenElems = []rune{}
charElems = []rune{}
idElems = []rune{}
}
}
fmt.Println()
fmt.Println()
runes_print(tokenElems)
runes_print(charElems)
runes_print(idElems)
}
type Char struct {
PositionInFile int // Nth char in the whole file
Value rune
WhereTheCharIsStored string
ErlangSourceWithoutFilePath bool
TokenDetected bool
TokenId int
TokenFirstCharPositionInFile int // the Token's first char's position in file
LineNum int
PositionInLine int // Nth char in the line
}
/*
A lot of different versions of a module can be loaded and prepared to be executed same time.
example situation: we have a 'database', 'financial' and 'logging' modules.
# the module version attrib is a string, totally free, how do you define them. The teams can find out their own versioning
# in the example, different teams are working with different modules,
# and they are unable to use the same version naming convention, to represent an unperfect situation
in the filesystem, the next versioned source codes are availabe (version name is a comment in the file)
'database' module available source code versions: "1.0.0 init", "1.1.0 connection fixes", "1.2.0 new db version"
'financial' module available source code versions: "1.0a draft", "1.0b first working"
'logging' module available source code versions: "2023_10_22_a original", "2023_10_25_b", "2023_12_01_a", "2023_12_01_c"
The architect defines the next executable version groups, where the 3 modules are able to work together:
in the example you can see, that the 'financial' module is same in both release,
but the database '1.0.0 init' can work only with 'logging' module's "2023_10_22_a original",
So you can load/define more, than 2 different executable version sets, for your modules.
FIXME: DEFINE EXACTLY WHERE the groups can be defined, and how is it handled
Basically Erlango works like Erlang: you can load your source codes, and exec them.
But if you define more version groups, more versions can be compiled/loaded/ready in the memmory,
and in your incoming requests you can define which version group can execute them.
With this solution, you can keep a lot of versions in the memory, and if you need,
you can revert your changes.
This versioning is different from hot code loading, because in that situation
you modify an 'Executable_version_group' in a running system, and you change one element,
which means that at the next func usage a new version will be executed.
Executable_version_groups = #{
"1_freely_defined_group_name" => [ ['database', "1.0.0 init"],
['financial', "1.0b first working"],
['logging', "2023_10_22_a original"]
],
"2_release_with_db_fixes" => [ ['database', "1.1.0 connection fixes"],
['financial', "1.0b first working"],
['logging', "2023_12_01_a"]
]
}
*/
type SourceTokensExecutables struct {
ModuleName string
// TODO: in source code loading, detect this, and set this attribute
ModuleVersion string // same module's different versions can be compiled and loaded same time
// in your erl files, you can set it:
// % ERLANGO_MODULE_VERSION your_version_definition
WhereTheCodeIsStored string
CharsFromErlFile Chars
Tokens ErlTokens // this section is filled by token detection part (step_01_tokens_from_source_code_of_files)
Expressions ErlExpressions // this section is filled by expression detection (step_02_expressions_from_tokens_from_lot_of_sources)
Errors errorsDetected
ErlangSourceWithoutFilePath bool // if this is true, the source was not coming from a file
}
type SourcesTokensExecutables_map map[string]SourceTokensExecutables
func (sourceTokensExecutables SourceTokensExecutables) tokens_print() {
// mode: testCasePrint
// mode: humanReading
print("=== Tokens in a file: ", sourceTokensExecutables.WhereTheCodeIsStored, "===\n")
// TODO: use a generic here
keys := make([]int, 0, len(sourceTokensExecutables.Tokens))
for k := range sourceTokensExecutables.Tokens {
keys = append(keys, k)
}
sort.Ints(keys)
for _, key := range keys {
token := sourceTokensExecutables.Tokens[key]
tokenPosFirst, tokenPosLast := token.charPositionFirstLast()
// fmt.Println(key, "token:", token.TokenId, token.TokenType, tokenPosFirst, tokenPosLast, token.stringRepresentation() )
// This format can be used in tests, immediatelly
stringRepresentation := token.stringRepresentation_escapedSourceForTests()
// TODO: stringrepresentation needs to be escaped " signs?
fmt.Printf("{\"%s\", %v, %v, \"%s\"},\n", token.TokenType, tokenPosFirst, tokenPosLast, stringRepresentation)
}
}
type errorsDetected []errorDetected
type errorDetected struct {
filePath string
lineNum int
charPosInLine int
charPosInFile int
errMsg string
}