-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lexer.hs
64 lines (59 loc) · 2.6 KB
/
Lexer.hs
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
{- This isn't a lexer in the sense that it provides a JavaScript token-stream.
- This module provides character-parsers for various JavaScript tokens.
-}
module WebBits.JavaScript.Lexer(lexeme,identifier,reserved,operator,reservedOp,charLiteral,
stringLiteral,natural,integer,float,naturalOrFloat,
decimal,hexadecimal,octal,symbol,whiteSpace,parens,
braces,brackets,squares,semi,comma,colon,dot,
identifierStart) where
import Prelude hiding (lex)
import Text.ParserCombinators.Parsec
import qualified Text.ParserCombinators.Parsec.Token as T
identifierStart = (letter <|> oneOf "$_")
javascriptDef =
T.LanguageDef "/*"
"*/"
"//"
False -- no nested comments
{- Adapted from syntax/regexps.ss in Dave's code. -}
identifierStart
(alphaNum <|> oneOf "$_") -- identifier rest
(oneOf "{}<>()~.,?:|&^=!+-*/%!") -- operator start
(oneOf "=<>|&+") -- operator rest
["break", "case", "catch", "const", "continue", "debugger",
"default", "delete", "do", "else", "enum", "false", "finally",
"for", "function", "if", "instanceof", "in", "let", "new",
"null", "return", "switch", "this", "throw", "true", "try",
"typeof", "var", "void", "while", "with"]
["|=", "^=", "&=", "<<=", ">>=", ">>>=", "+=", "-=", "*=", "/=",
"%=", "=", ";", ",", "?", ":", "||", "&&", "|", "^", "&",
"===", "==", "=", "!==", "!=", "<<", "<=", "<", ">>>", ">>",
">=", ">", "++", "--", "+", "-", "*", "/", "%", "!", "~", ".",
"[", "]", "{", "}", "(", ")","</","instanceof"]
True -- case-sensitive
lex = T.makeTokenParser javascriptDef
-- everything but commaSep and semiSep
identifier = T.identifier lex
reserved = T.reserved lex
operator = T.operator lex
reservedOp = T.reservedOp lex
charLiteral = T.charLiteral lex
stringLiteral = T.stringLiteral lex
natural = T.natural lex
integer = T.integer lex
float = T.float lex
naturalOrFloat = T.naturalOrFloat lex
decimal = T.decimal lex
hexadecimal = T.hexadecimal lex
octal = T.octal lex
symbol = T.symbol lex
whiteSpace = T.whiteSpace lex
parens = T.parens lex
braces = T.braces lex
squares = T.squares lex
semi = T.semi lex
comma = T.comma lex
colon = T.colon lex
dot = T.dot lex
brackets = T.brackets lex
lexeme = T.lexeme lex