@@ -59,31 +59,32 @@ function isInt(src: string) {
59
59
* @param watchMode Whether is it watch mode or not. Errors will throw an error instead of exiting if this value is set to `true`.
60
60
* @returns A list of tokens generated from source string.
61
61
* @author efekos
62
- * @version 1.0.0
62
+ * @version 1.0.1
63
63
* @since 0.0.1-alpha
64
64
*/
65
65
export function tokenizeSyx ( source : string , watchMode : boolean ) : Token [ ] {
66
66
const tokens : Token [ ] = [ ] ;
67
67
const src = source . split ( '' ) ;
68
68
let curPos = - 1 ;
69
+ let curLine = 0 ;
69
70
70
71
while ( src . length > 0 ) {
71
72
if ( ! isSkippable ( src [ 0 ] ) ) log . debug ( `Parsing token: '${ src [ 0 ] } '` ) ;
72
- if ( src [ 0 ] === '(' ) tokens . push ( { type : TokenType . OpenParen , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
73
- else if ( src [ 0 ] === ')' ) tokens . push ( { type : TokenType . CloseParen , value : src . shift ( ) , pos :++ curPos , end : curPos } ) ;
74
- else if ( src [ 0 ] === '{' ) tokens . push ( { type : TokenType . OpenBrace , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
75
- else if ( src [ 0 ] === '}' ) tokens . push ( { type : TokenType . CloseBrace , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
76
- else if ( src [ 0 ] === '[' ) tokens . push ( { type : TokenType . OpenSquare , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
77
- else if ( src [ 0 ] === ']' ) tokens . push ( { type : TokenType . CloseSquare , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
78
- else if ( src [ 0 ] === ',' ) tokens . push ( { type : TokenType . Comma , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
79
- else if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
80
- else if ( src [ 0 ] === '<' ) tokens . push ( { type : TokenType . OpenDiamond , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
81
- else if ( src [ 0 ] === '>' ) tokens . push ( { type : TokenType . CloseDiamond , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
82
- else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
83
- else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
84
- else if ( src [ 0 ] === '|' ) tokens . push ( { type : TokenType . VarSeperator , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
73
+ if ( src [ 0 ] === '(' ) tokens . push ( { type : TokenType . OpenParen , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
74
+ else if ( src [ 0 ] === ')' ) tokens . push ( { type : TokenType . CloseParen , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
75
+ else if ( src [ 0 ] === '{' ) tokens . push ( { type : TokenType . OpenBrace , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
76
+ else if ( src [ 0 ] === '}' ) tokens . push ( { type : TokenType . CloseBrace , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
77
+ else if ( src [ 0 ] === '[' ) tokens . push ( { type : TokenType . OpenSquare , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
78
+ else if ( src [ 0 ] === ']' ) tokens . push ( { type : TokenType . CloseSquare , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
79
+ else if ( src [ 0 ] === ',' ) tokens . push ( { type : TokenType . Comma , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
80
+ else if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
81
+ else if ( src [ 0 ] === '<' ) tokens . push ( { type : TokenType . OpenDiamond , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
82
+ else if ( src [ 0 ] === '>' ) tokens . push ( { type : TokenType . CloseDiamond , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
83
+ else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
84
+ else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
85
+ else if ( src [ 0 ] === '|' ) tokens . push ( { type : TokenType . VarSeperator , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
85
86
else if ( src [ 0 ] === '+' && chars . includes ( src [ 1 ] ) ) {
86
- if ( src [ 1 ] === 's' ) tokens . push ( { type : TokenType . WhitespaceIdentifier , value : '+s' , pos :++ curPos , end :curPos + 1 } ) ;
87
+ if ( src [ 1 ] === 's' ) tokens . push ( { type : TokenType . WhitespaceIdentifier , value : '+s' , pos : ++ curPos , end : curPos + 1 , line : curLine } ) ;
87
88
else ( watchMode ? log . thrower : log . exit ) . error ( `${ chalk . gray ( curPos ) } Unexpected identifier: '${ src [ 1 ] } '` ) ;
88
89
src . shift ( ) ; src . shift ( ) ;
89
90
} else if ( isInt ( src [ 0 ] ) ) {
@@ -95,7 +96,7 @@ export function tokenizeSyx(source: string, watchMode: boolean): Token[] {
95
96
curPos ++ ;
96
97
}
97
98
98
- tokens . push ( { type : TokenType . IntNumber , value : ident , pos :startPos , end : curPos } ) ;
99
+ tokens . push ( { type : TokenType . IntNumber , value : ident , pos : startPos , end : curPos , line : curLine } ) ;
99
100
} else if ( isAlphabetic ( src [ 0 ] ) ) {
100
101
log . debug ( 'Found identifier' ) ;
101
102
let ident = '' ;
@@ -107,12 +108,17 @@ export function tokenizeSyx(source: string, watchMode: boolean): Token[] {
107
108
108
109
const reserved = keywords [ ident ] ;
109
110
if ( reserved !== undefined ) log . debug ( `Found keyword: '${ reserved } '` ) ;
110
- tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident , pos :startPos , end :curPos } ) ;
111
- } else if ( isSkippable ( src [ 0 ] ) ) { log . debug ( 'Found skippable char' ) ; src . shift ( ) ; curPos ++ ; }
112
- else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
111
+ tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident , pos : startPos , end : curPos , line : curLine } ) ;
112
+ } else if ( isSkippable ( src [ 0 ] ) ) {
113
+ log . debug ( 'Found skippable char' ) ;
114
+ src . shift ( ) ;
115
+ curPos ++ ;
116
+ if ( src [ 0 ] === '\n' ) curLine ++ ;
117
+ }
118
+ else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
113
119
}
114
120
115
- tokens . push ( { type : TokenType . EndOfFile , value : 'EOF' , pos :source . length , end :source . length } ) ;
121
+ tokens . push ( { type : TokenType . EndOfFile , value : 'EOF' , pos : source . length , end : source . length , line : curLine } ) ;
116
122
return tokens ;
117
123
}
118
124
@@ -121,20 +127,21 @@ export function tokenizeSyx(source: string, watchMode: boolean): Token[] {
121
127
* @param source Source string.
122
128
* @returns A list of tokens generated from th esource file.
123
129
* @author efekos
124
- * @version 1.0.0
130
+ * @version 1.0.1
125
131
* @since 0.0.1-alpha
126
132
*/
127
133
export function tokenizeSys ( source : string ) : Token [ ] {
128
134
const src = source . split ( '' ) ;
129
135
const tokens : Token [ ] = [ ] ;
130
-
136
+
131
137
let curPos = - 1 ;
138
+ let curLine = 0 ;
132
139
133
140
while ( src . length > 0 && `${ src [ 0 ] } ${ src [ 1 ] } ${ src [ 2 ] } ` !== ':::' ) {
134
141
if ( ! isSkippable ( src [ 0 ] ) ) log . debug ( `Parsing tokenmm: '${ src [ 0 ] } '` ) ;
135
- if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
136
- else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
137
- else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
142
+ if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
143
+ else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
144
+ else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
138
145
else if ( isAlphabetic ( src [ 0 ] ) ) {
139
146
log . debug ( 'Found identifier' ) ;
140
147
let ident = '' ;
@@ -146,12 +153,17 @@ export function tokenizeSys(source: string): Token[] {
146
153
147
154
const reserved = keywords [ ident ] ;
148
155
if ( reserved !== undefined ) log . debug ( `Found keyword: '${ reserved } '` ) ;
149
- tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident , pos :startPost , end :curPos } ) ;
150
- } else if ( isSkippable ( src [ 0 ] ) ) { log . debug ( 'Found skippable char' ) ; src . shift ( ) ; curPos ++ ; }
151
- else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) , pos :++ curPos , end :curPos } ) ;
156
+ tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident , pos : startPost , end : curPos , line : curLine } ) ;
157
+ } else if ( isSkippable ( src [ 0 ] ) ) {
158
+ log . debug ( 'Found skippable char' ) ;
159
+ src . shift ( ) ;
160
+ curPos ++ ;
161
+ if ( src [ 0 ] === '\n' ) curLine ++ ;
162
+ }
163
+ else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) , pos : ++ curPos , end : curPos , line : curLine } ) ;
152
164
153
165
}
154
166
155
- tokens . push ( { type : TokenType . EndOfFile , value : 'eof' , pos :++ curPos , end :curPos } ) ;
167
+ tokens . push ( { type : TokenType . EndOfFile , value : 'eof' , pos : ++ curPos , end : curPos , line : curLine } ) ;
156
168
return tokens ;
157
169
}
0 commit comments