A JavaScript lexer and syntax highlighter for Gleam!

Add token type

+313
+313
src/just/token.gleam
··· 1 + pub type Token { 2 + // Comments and whitespace 3 + SingleLineComment(String) 4 + MultiLineComment(String) 5 + HashBangComment(String) 6 + Whitespace(String) 7 + 8 + // Literals 9 + Identifier(String) 10 + PrivateIdentifier(String) 11 + Number(String) 12 + BigInt(String) 13 + String(quote: String, contents: String) 14 + RegularExpression(String) 15 + TemplateHead(tag: String, contents: String) 16 + TemplateMiddle(String) 17 + TemplateTail(String) 18 + 19 + // Keywords 20 + Break 21 + Case 22 + Catch 23 + Class 24 + Const 25 + Continue 26 + Debugger 27 + Default 28 + Delete 29 + Do 30 + Else 31 + Export 32 + Extends 33 + False 34 + Finally 35 + For 36 + Function 37 + If 38 + Import 39 + In 40 + Instanceof 41 + New 42 + Null 43 + Return 44 + Super 45 + Switch 46 + This 47 + Throw 48 + True 49 + Try 50 + Typeof 51 + Var 52 + Void 53 + While 54 + With 55 + 56 + // Keywords in strict mode 57 + Let 58 + Static 59 + Yield 60 + 61 + // Keywords in an async function 62 + Await 63 + 64 + // Future reserved words 65 + Enum 66 + 67 + // Future reserved words in strict mode 68 + Implements 69 + Interface 70 + Package 71 + Private 72 + Protected 73 + 74 + // Contextual keywords 75 + ContextualKeyword(ContextualKeyword) 76 + 77 + // Grouping 78 + LeftBrace 79 + RightBrace 80 + LeftParen 81 + RightParen 82 + LeftSquare 83 + RightSquare 84 + 85 + // Separators 86 + Dot 87 + TripleDot 88 + Semicolon 89 + Comma 90 + Colon 91 + Arrow 92 + 93 + // Comparison 94 + Less 95 + Greater 96 + LessEqual 97 + GreaterEqual 98 + DoubleEqual 99 + BangEqual 100 + TripleEqual 101 + BangDoubleEqual 102 + 103 + // Arithmetic 104 + Plus 105 + Minus 106 + Star 107 + Slash 108 + Percent 109 + DoubleStar 110 + DoublePlus 111 + DoubleMinus 112 + DoubleLess 113 + DoubleGreater 114 + TripleGreater 115 + Ampersand 116 + Pipe 117 + Caret 118 + Tilde 119 + 120 + // Logic 121 + Bang 122 + DoubleAmpersand 123 + DoublePipe 124 + Question 125 + DoubleQuestion 126 + QuestionDot 127 + 128 + // Assignment 129 + Equal 130 + PlusEqual 131 + MinusEqual 132 + StarEqual 133 + SlashEqual 134 + PercentEqual 135 + DoubleStarEqual 136 + DoubleLessEqual 137 + DoubleGreaterEqual 138 + TripleGreaterEqual 139 + AmpersandEqual 140 + PipeEqual 141 + CaratEqual 142 + DoubleAmpersandEqual 143 + DoublePipeEqual 144 + DoubleQuestionEqual 145 + } 146 + 147 + pub type ContextualKeyword { 148 + As 149 + Async 150 + From 151 + Get 152 + Of 153 + Set 154 + } 155 + 156 + pub fn contextual_keyword_name(keyword: ContextualKeyword) -> String { 157 + case keyword { 158 + As -> "as" 159 + Async -> "async" 160 + From -> "from" 161 + Get -> "get" 162 + Of -> "of" 163 + Set -> "set" 164 + } 165 + } 166 + 167 + pub fn to_source(token: Token) -> String { 168 + case token { 169 + // Comments and whitespace 170 + SingleLineComment(value) -> "//" <> value 171 + MultiLineComment(value) -> "/*" <> value <> "*/" 172 + HashBangComment(value) -> "#!" <> value 173 + Whitespace(value) -> value 174 + 175 + // Literals 176 + Identifier(value) -> value 177 + PrivateIdentifier(value) -> "#" <> value 178 + Number(value) -> value 179 + BigInt(value) -> value <> "n" 180 + String(quote:, contents:) -> quote <> contents <> quote 181 + RegularExpression(value) -> "/" <> value <> "/" 182 + TemplateHead(tag:, contents:) -> tag <> "`" <> contents 183 + TemplateMiddle(value) -> value 184 + TemplateTail(value) -> value <> "`" 185 + 186 + // Keywords 187 + Break -> "break" 188 + Case -> "case" 189 + Catch -> "catch" 190 + Class -> "class" 191 + Const -> "const" 192 + Continue -> "continue" 193 + Debugger -> "debugger" 194 + Default -> "default" 195 + Delete -> "delete" 196 + Do -> "do" 197 + Else -> "else" 198 + Export -> "export" 199 + Extends -> "extends" 200 + False -> "false" 201 + Finally -> "finally" 202 + For -> "for" 203 + Function -> "function" 204 + If -> "if" 205 + Import -> "import" 206 + In -> "in" 207 + Instanceof -> "instanceof" 208 + New -> "new" 209 + Null -> "null" 210 + Return -> "return" 211 + Super -> "super" 212 + Switch -> "switch" 213 + This -> "this" 214 + Throw -> "throw" 215 + True -> "true" 216 + Try -> "try" 217 + Typeof -> "typeof" 218 + Var -> "var" 219 + Void -> "void" 220 + While -> "while" 221 + With -> "with" 222 + 223 + // Keywords in strict mode 224 + Let -> "let" 225 + Static -> "static" 226 + Yield -> "yield" 227 + 228 + // Keywords in an async function 229 + Await -> "await" 230 + 231 + // Future reserved words 232 + Enum -> "enum" 233 + 234 + // Future reserved words in strict mode 235 + Implements -> "implements" 236 + Interface -> "interface" 237 + Package -> "package" 238 + Private -> "private" 239 + Protected -> "protected" 240 + 241 + // Contextual keywords 242 + ContextualKeyword(keyword) -> contextual_keyword_name(keyword) 243 + 244 + // Grouping 245 + LeftBrace -> "{" 246 + RightBrace -> "}" 247 + LeftParen -> "(" 248 + RightParen -> ")" 249 + LeftSquare -> "[" 250 + RightSquare -> "]" 251 + 252 + // Separators 253 + Dot -> "." 254 + TripleDot -> "..." 255 + Semicolon -> ";" 256 + Comma -> "," 257 + Colon -> ":" 258 + Arrow -> "=>" 259 + 260 + // Comparison 261 + Less -> "<" 262 + Greater -> ">" 263 + LessEqual -> "<=" 264 + GreaterEqual -> ">=" 265 + DoubleEqual -> "==" 266 + BangEqual -> "!=" 267 + TripleEqual -> "===" 268 + BangDoubleEqual -> "!==" 269 + 270 + // Arithmetic 271 + Plus -> "+" 272 + Minus -> "-" 273 + Star -> "*" 274 + Slash -> "/" 275 + Percent -> "%" 276 + DoubleStar -> "**" 277 + DoublePlus -> "++" 278 + DoubleMinus -> "--" 279 + DoubleLess -> "<<" 280 + DoubleGreater -> ">>" 281 + TripleGreater -> ">>>" 282 + Ampersand -> "&" 283 + Pipe -> "|" 284 + Caret -> "^" 285 + Tilde -> "~" 286 + 287 + // Logic 288 + Bang -> "!" 289 + DoubleAmpersand -> "&&" 290 + DoublePipe -> "||" 291 + Question -> "?" 292 + DoubleQuestion -> "??" 293 + QuestionDot -> "?." 294 + 295 + // Assignment 296 + Equal -> "=" 297 + PlusEqual -> "+=" 298 + MinusEqual -> "-=" 299 + StarEqual -> "*=" 300 + SlashEqual -> "/=" 301 + PercentEqual -> "%=" 302 + DoubleStarEqual -> "**=" 303 + DoubleLessEqual -> "<<=" 304 + DoubleGreaterEqual -> ">>=" 305 + TripleGreaterEqual -> ">>>=" 306 + AmpersandEqual -> "&=" 307 + PipeEqual -> "|=" 308 + CaratEqual -> "^=" 309 + DoubleAmpersandEqual -> "&&=" 310 + DoublePipeEqual -> "||=" 311 + DoubleQuestionEqual -> "??=" 312 + } 313 + }