Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement parsing for octal numbers.

  • Loading branch information...
commit 991d05e9dc84f5e76aa3b6ca60fe56086a876267 1 parent 864e762
@aszlig aszlig authored
View
4 runtests.hs
@@ -65,6 +65,8 @@ testSuite = testGroup "Parser"
, testCase "LiteralDecimal14" (testLiteral "1e+18" "Right (JSDecimal \"1e+18\")")
, testCase "LiteralDecimal15" (testLiteral "1e-18" "Right (JSDecimal \"1e-18\")")
+ , testCase "LiteralOctal" (testLiteral "010" "Right (JSOctal \"010\")")
+
, testCase "LiteralString1" (testLiteral "\"hello\\nworld\"" "Right (JSStringLiteral '\"' \"hello\\\\nworld\")")
, testCase "LiteralString2" (testLiteral "'hello\\nworld'" "Right (JSStringLiteral '\\'' \"hello\\\\nworld\")")
@@ -358,6 +360,7 @@ commentSuite = testGroup "Comments"
, testCase "LiteralHexInteger" (testLiteralC "/*d*/0x1234fF" "Right (NS (JSHexInteger \"0x1234fF\") (TokenPn 0 1 1) [CommentA (TokenPn 0 1 1) \"/*d*/\"])")
, testCase "LiteralDecimal" (testLiteralC "/*e*/1.0e4" "Right (NS (JSDecimal \"1.0e4\") (TokenPn 0 1 1) [CommentA (TokenPn 0 1 1) \"/*e*/\"])")
+ , testCase "LiteralOctal" (testLiteralC "/*x*/011" "Right (NS (JSOctal \"011\") (TokenPn 0 1 1) [CommentA (TokenPn 0 1 1) \"/*x*/\"])")
, testCase "LiteralString1" (testLiteralC "/*f*/\"hello\\nworld\"" "Right (NS (JSStringLiteral '\"' \"hello\\\\nworld\") (TokenPn 0 1 1) [CommentA (TokenPn 0 1 1) \"/*f*/\"])")
, testCase "LiteralString2" (testLiteralC "/*g*/'hello\\nworld'" "Right (NS (JSStringLiteral '\\'' \"hello\\\\nworld\") (TokenPn 0 1 1) [CommentA (TokenPn 0 1 1) \"/*g*/\"])")
@@ -439,6 +442,7 @@ commentPrintSuite = testGroup "Comments"
, testCase "LiteralHexInteger" (testRoundTrip "/*d*/0x1234fF")
, testCase "LiteralDecimal" (testRoundTrip "/*e*/1.0e4")
+ , testCase "LiteralOctal" (testRoundTrip "/*x*/011")
, testCase "LiteralString1" (testRoundTrip "/*f*/\"hello\\nworld\"")
, testCase "LiteralString2" (testRoundTrip "/*g*/'hello\\nworld'")
View
7 src-dev/Language/JavaScript/Parser/Lexer.x
@@ -73,6 +73,8 @@ $short_str_char = [^ \n \r ' \" \\]
-- {Hex Digit} = {Digit} + [ABCDEF] + [abcdef]
@HexDigit = $digit | [a-fA-F]
+-- {Oct Digit} = {Digit} + [012345678]
+@OctDigit = $digit | [0-8]
-- {RegExp Chars} = {Letter}+{Digit}+['^']+['$']+['*']+['+']+['?']+['{']+['}']+['|']+['-']+['.']+[',']+['#']+['[']+[']']+['_']+['<']+['>']
--$RegExpChars = [$alpha $digit \^\$\*\+\?\{\}\|\-\.\,\#\[\]\_\<\>]
--$RegExpChars = [$printable] # [\\]
@@ -214,6 +216,9 @@ tokens :-
-- HexIntegerLiteral = '0x' {Hex Digit}+
<reg,divide> ("0x"|"0X") @HexDigit+ { adapt (mkString hexIntegerToken) }
+-- OctalLiteral = '0' {Octal Digit}+
+<reg,divide> ("0") @OctDigit+ { adapt (mkString octalToken) }
+
-- RegExp = '/' ({RegExp Chars} | '\' {Non Terminator})+ '/' ( 'g' | 'i' | 'm' )*
-- <reg> "/" ($RegExpChars | "\" $NonTerminator)+ "/" ("g"|"i"|"m")* { mkString regExToken }
@@ -327,7 +332,7 @@ classifyToken aToken =
TrueToken {} -> divide
FalseToken {} -> divide
ThisToken {} -> divide
- -- OctalToken {} -> divide -- May have to extend parser to cope with these
+ OctalToken {} -> divide
DecimalToken {} -> divide
HexIntegerToken {} -> divide
StringToken {} -> divide
View
2  src/Language/JavaScript/Parser/AST.hs
@@ -29,6 +29,7 @@ data Node =
| JSDecimal String
| JSLiteral String
| JSHexInteger String
+ | JSOctal String
| JSStringLiteral Char [Char]
| JSRegEx String
@@ -121,6 +122,7 @@ showStrippedNode (JSFunction _f x1 _lb x2s _rb x3) = "JSFunction (" ++ ss x1 ++
--showStrippedNode (JSFunctionBody xs) = "JSFunctionBody " ++ sss xs
showStrippedNode (JSFunctionExpression _f x1s _lb x2s _rb x3) = "JSFunctionExpression " ++ sss x1s ++ " " ++ sss x2s ++ " (" ++ ss x3 ++ ")"
showStrippedNode (JSHexInteger s) = "JSHexInteger " ++ show s
+showStrippedNode (JSOctal s) = "JSOctal " ++ show s
showStrippedNode (JSIdentifier s) = "JSIdentifier " ++ show s
showStrippedNode (JSIf _i _lb x1 _rb x2s x3s) = "JSIf (" ++ ss x1 ++ ") (" ++ sss x2s ++ ") (" ++ sss x3s ++ ")"
showStrippedNode (JSLabelled x1 _c x2) = "JSLabelled (" ++ ss x1 ++ ") (" ++ ss x2 ++ ")"
View
2  src/Language/JavaScript/Parser/Grammar.y
@@ -104,6 +104,7 @@ import qualified Language.JavaScript.Parser.AST as AST
'ident' { IdentifierToken {} }
'decimal' { DecimalToken {} }
'hexinteger' { HexIntegerToken {} }
+ 'octal' { OctalToken {} }
'string' { StringToken {} }
'regex' { RegExToken {} }
'assign' { AssignToken {} }
@@ -181,6 +182,7 @@ BooleanLiteral : 'true' { AST.JSLiteral "true" }
-- | HexIntegerLiteral
NumericLiteral : 'decimal' { AST.JSDecimal (token_literal $1)}
| 'hexinteger' { AST.JSHexInteger (token_literal $1)}
+ | 'octal' { AST.JSOctal (token_literal $1)}
StringLiteral : 'string' {AST.JSStringLiteral (token_delimiter $1) (token_literal $1)}
View
3  src/Language/JavaScript/Parser/Grammar5.y
@@ -110,6 +110,7 @@ import qualified Language.JavaScript.Parser.AST as AST
'ident' { IdentifierToken {} }
'decimal' { DecimalToken {} }
'hexinteger' { HexIntegerToken {} }
+ 'octal' { OctalToken {} }
'string' { StringToken {} }
'regex' { RegExToken {} }
'assign' { AssignToken {} }
@@ -345,9 +346,11 @@ BooleanLiteral : 'true' { fp (AST.NT (AST.JSLiteral "true") (ss $1) (gc $1)) }
-- <Numeric Literal> ::= DecimalLiteral
-- | HexIntegerLiteral
+-- | OctalLiteral
NumericLiteral :: { AST.JSNode }
NumericLiteral : 'decimal' { fp (AST.NT (AST.JSDecimal (token_literal $1)) (ss $1) (gc $1))}
| 'hexinteger' { fp (AST.NT (AST.JSHexInteger (token_literal $1)) (ss $1) (gc $1)) }
+ | 'octal' { fp (AST.NT (AST.JSOctal (token_literal $1)) (ss $1) (gc $1)) }
StringLiteral :: { AST.JSNode }
StringLiteral : 'string' { fp (AST.NT (AST.JSStringLiteral (token_delimiter $1) (token_literal $1)) (ss $1) (gc $1)) }
View
4 src/Language/JavaScript/Parser/LexerUtils.hs
@@ -25,6 +25,7 @@ module Language.JavaScript.Parser.LexerUtils (
, endOfFileToken
, assignToken
, hexIntegerToken
+ , octalToken
, stringToken
--, lexicalError
) where
@@ -55,6 +56,9 @@ decimalToken loc str = DecimalToken loc str []
hexIntegerToken :: TokenPosn -> String -> Token
hexIntegerToken loc str = HexIntegerToken loc str []
+octalToken :: TokenPosn -> String -> Token
+octalToken loc str = OctalToken loc str []
+
assignToken :: TokenPosn -> String -> Token
assignToken loc str = AssignToken loc str []
View
2  src/Language/JavaScript/Parser/Token.hs
@@ -46,6 +46,8 @@ data Token
-- ^ Literal: Decimal
| HexIntegerToken { token_span :: !TokenPosn, token_literal :: !String, token_comment :: ![CommentAnnotation] }
-- ^ Literal: Hexadecimal Integer
+ | OctalToken { token_span :: !TokenPosn, token_literal :: !String, token_comment :: ![CommentAnnotation] }
+ -- ^ Literal: Octal Integer
| StringToken { token_span :: !TokenPosn, token_literal :: !String, token_delimiter :: !Char, token_comment :: ![CommentAnnotation] }
-- ^ Literal: string, delimited by either single or double quotes
| RegExToken { token_span :: !TokenPosn, token_literal :: !String, token_comment :: ![CommentAnnotation] }
View
1  src/Language/JavaScript/Pretty/Printer.hs
@@ -74,6 +74,7 @@ rn (NT (JSIdentifier s ) p cs) foo = rcs cs p s foo
rn (NT (JSDecimal i ) p cs) foo = rcs cs p i foo
rn (NT (JSLiteral l ) p cs) foo = rcs cs p l foo
rn (NT (JSHexInteger i ) p cs) foo = rcs cs p i foo
+rn (NT (JSOctal i ) p cs) foo = rcs cs p i foo
rn (NT (JSStringLiteral s l) p cs) foo = rcs cs p ((s:l)++[s]) foo
rn (NT (JSRegEx s ) p cs) foo = rcs cs p s foo
Please sign in to comment.
Something went wrong with that request. Please try again.