1
1
import { Token , TokenType } from './types.js' ;
2
2
import { log } from '../log.js' ;
3
+ import chalk from 'chalk' ;
3
4
4
5
const keywords : Record < string , TokenType > = {
5
6
operator : TokenType . OperatorKeyword ,
@@ -64,49 +65,54 @@ function isInt(src: string) {
64
65
export function tokenizeSyx ( source : string , watchMode : boolean ) : Token [ ] {
65
66
const tokens : Token [ ] = [ ] ;
66
67
const src = source . split ( '' ) ;
68
+ let curPos = - 1 ;
67
69
68
70
while ( src . length > 0 ) {
69
71
if ( ! isSkippable ( src [ 0 ] ) ) log . debug ( `Parsing token: '${ src [ 0 ] } '` ) ;
70
- if ( src [ 0 ] === '(' ) tokens . push ( { type : TokenType . OpenParen , value : src . shift ( ) } ) ;
71
- else if ( src [ 0 ] === ')' ) tokens . push ( { type : TokenType . CloseParen , value : src . shift ( ) } ) ;
72
- else if ( src [ 0 ] === '{' ) tokens . push ( { type : TokenType . OpenBrace , value : src . shift ( ) } ) ;
73
- else if ( src [ 0 ] === '}' ) tokens . push ( { type : TokenType . CloseBrace , value : src . shift ( ) } ) ;
74
- else if ( src [ 0 ] === '[' ) tokens . push ( { type : TokenType . OpenSquare , value : src . shift ( ) } ) ;
75
- else if ( src [ 0 ] === ']' ) tokens . push ( { type : TokenType . CloseSquare , value : src . shift ( ) } ) ;
76
- else if ( src [ 0 ] === ',' ) tokens . push ( { type : TokenType . Comma , value : src . shift ( ) } ) ;
77
- else if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) } ) ;
78
- else if ( src [ 0 ] === '<' ) tokens . push ( { type : TokenType . OpenDiamond , value : src . shift ( ) } ) ;
79
- else if ( src [ 0 ] === '>' ) tokens . push ( { type : TokenType . CloseDiamond , value : src . shift ( ) } ) ;
80
- else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) } ) ;
81
- else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) } ) ;
82
- else if ( src [ 0 ] === '|' ) tokens . push ( { type : TokenType . VarSeperator , value : src . shift ( ) } ) ;
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 } ) ;
83
85
else if ( src [ 0 ] === '+' && chars . includes ( src [ 1 ] ) ) {
84
- if ( src [ 1 ] === 's' ) tokens . push ( { type : TokenType . WhitespaceIdentifier , value : '+s' } ) ;
85
- else ( watchMode ? log . thrower : log . exit ) . error ( `Unexpected identifier: '${ src [ 1 ] } '` ) ;
86
+ if ( src [ 1 ] === 's' ) tokens . push ( { type : TokenType . WhitespaceIdentifier , value : '+s' , pos : ++ curPos , end : curPos + 1 } ) ;
87
+ else ( watchMode ? log . thrower : log . exit ) . error ( `${ chalk . gray ( curPos ) } Unexpected identifier: '${ src [ 1 ] } '` ) ;
86
88
src . shift ( ) ; src . shift ( ) ;
87
89
} else if ( isInt ( src [ 0 ] ) ) {
88
90
log . debug ( 'Found int number' ) ;
89
91
let ident = '' ;
92
+ const startPos = ++ curPos ;
90
93
while ( src . length > 0 && isInt ( src [ 0 ] ) ) {
91
94
ident += src . shift ( ) ;
95
+ curPos ++ ;
92
96
}
93
97
94
- tokens . push ( { type : TokenType . IntNumber , value : ident } ) ;
98
+ tokens . push ( { type : TokenType . IntNumber , value : ident , pos : startPos , end : curPos } ) ;
95
99
} else if ( isAlphabetic ( src [ 0 ] ) ) {
96
100
log . debug ( 'Found identifier' ) ;
97
101
let ident = '' ;
102
+ const startPos = ++ curPos ;
98
103
while ( src . length > 0 && isAlphabetic ( src [ 0 ] ) ) {
99
104
ident += src . shift ( ) ;
105
+ curPos ++ ;
100
106
}
101
107
102
108
const reserved = keywords [ ident ] ;
103
109
if ( reserved !== undefined ) log . debug ( `Found keyword: '${ reserved } '` ) ;
104
- tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident } ) ;
105
- } else if ( isSkippable ( src [ 0 ] ) ) { log . debug ( 'Found skippable char' ) ; src . shift ( ) ; }
106
- else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) } ) ;
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 } ) ;
107
113
}
108
114
109
- tokens . push ( { type : TokenType . EndOfFile , value : 'EOF' } ) ;
115
+ tokens . push ( { type : TokenType . EndOfFile , value : 'EOF' , pos : source . length , end : source . length } ) ;
110
116
return tokens ;
111
117
}
112
118
@@ -121,27 +127,31 @@ export function tokenizeSyx(source: string, watchMode: boolean): Token[] {
121
127
export function tokenizeSys ( source : string ) : Token [ ] {
122
128
const src = source . split ( '' ) ;
123
129
const tokens : Token [ ] = [ ] ;
130
+
131
+ let curPos = - 1 ;
124
132
125
133
while ( src . length > 0 && `${ src [ 0 ] } ${ src [ 1 ] } ${ src [ 2 ] } ` !== ':::' ) {
126
134
if ( ! isSkippable ( src [ 0 ] ) ) log . debug ( `Parsing tokenmm: '${ src [ 0 ] } '` ) ;
127
- if ( src [ 0 ] === ';' ) tokens . push ( { type : TokenType . Semicolon , value : src . shift ( ) } ) ;
128
- else if ( src [ 0 ] === '\'' ) tokens . push ( { type : TokenType . SingleQuote , value : src . shift ( ) } ) ;
129
- else if ( src [ 0 ] === '"' ) tokens . push ( { type : TokenType . DoubleQuote , value : src . shift ( ) } ) ;
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 } ) ;
130
138
else if ( isAlphabetic ( src [ 0 ] ) ) {
131
139
log . debug ( 'Found identifier' ) ;
132
140
let ident = '' ;
141
+ const startPost = ++ curPos ;
133
142
while ( src . length > 0 && isAlphabetic ( src [ 0 ] ) ) {
134
143
ident += src . shift ( ) ;
144
+ curPos ++ ;
135
145
}
136
146
137
147
const reserved = keywords [ ident ] ;
138
148
if ( reserved !== undefined ) log . debug ( `Found keyword: '${ reserved } '` ) ;
139
- tokens . push ( { type : reserved ?? TokenType . Identifier , value : ident } ) ;
140
- } else if ( isSkippable ( src [ 0 ] ) ) { log . debug ( 'Found skippable char' ) ; src . shift ( ) ; }
141
- else tokens . push ( { type : TokenType . Raw , value : src . shift ( ) } ) ;
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 } ) ;
142
152
143
153
}
144
154
145
- tokens . push ( { type : TokenType . EndOfFile , value : 'eof' } ) ;
155
+ tokens . push ( { type : TokenType . EndOfFile , value : 'eof' , pos : ++ curPos , end : curPos } ) ;
146
156
return tokens ;
147
157
}
0 commit comments