-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
correctly parse enum values #104
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
declare module valueExpression { | ||
export enum StringEnum { | ||
A = "test", | ||
B = A, | ||
C = "test" + "test" | ||
} | ||
export enum NumberEnum { | ||
A = 3 + 3, | ||
B = 1 << 3, | ||
C = 10**2+1*8- - -3/3>>2<<3>>>+19%~~~3+ + +9 | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
import scala.scalajs.js | ||
import js.annotation._ | ||
import js.| | ||
|
||
package valueExpression { | ||
|
||
package valueExpression { | ||
|
||
@js.native | ||
sealed trait StringEnum extends js.Object { | ||
} | ||
|
||
@js.native | ||
@JSGlobal("valueExpression.StringEnum") | ||
object StringEnum extends js.Object { | ||
var A: StringEnum = js.native | ||
var B: StringEnum = js.native | ||
var C: StringEnum = js.native | ||
@JSBracketAccess | ||
def apply(value: StringEnum): String = js.native | ||
} | ||
|
||
@js.native | ||
sealed trait NumberEnum extends js.Object { | ||
} | ||
|
||
@js.native | ||
@JSGlobal("valueExpression.NumberEnum") | ||
object NumberEnum extends js.Object { | ||
var A: NumberEnum = js.native | ||
var B: NumberEnum = js.native | ||
var C: NumberEnum = js.native | ||
@JSBracketAccess | ||
def apply(value: NumberEnum): String = js.native | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,6 +46,18 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions { | |||||
"...", "=>" | ||||||
) | ||||||
|
||||||
// for value expressions | ||||||
val binaryValueOperators = Set( | ||||||
"+", "-", "*", "/", "**", "%", | ||||||
"<<", ">>>", ">>", "&", "|", "^" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 lines should be indented with 4 spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And given my comments below, consider just including those directly inside |
||||||
) | ||||||
lexical.delimiters ++= binaryValueOperators | ||||||
|
||||||
val unaryValueOperators = Set( | ||||||
"+", "-", "~" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should be indented with 4 spaces. |
||||||
) | ||||||
lexical.delimiters ++= unaryValueOperators | ||||||
|
||||||
def parseDefinitions(input: Reader[Char]) = | ||||||
phrase(ambientDeclarations)(new lexical.Scanner(input)) | ||||||
|
||||||
|
@@ -105,7 +117,20 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions { | |||||
"enum" ~> typeName ~ ("{" ~> ambientEnumBody <~ "}") ^^ EnumDecl | ||||||
|
||||||
lazy val ambientEnumBody: Parser[List[Ident]] = | ||||||
repsep(identifier <~ opt("=" ~ (numericLit | stringLit) ), ",") <~ opt(",") | ||||||
repsep(identifier <~ opt("=" ~! valueExpression), ",") <~ opt(",") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they are non backtracking operators. basically, after the I was already about to make a pr which added many more of these operators but it's not always totally clear where to introduce these cuts while preserving the semantics and I want to make sure that I am correct. I will make the pr in a few days |
||||||
|
||||||
lazy val valueExpression: Parser[_] = | ||||||
rep1sep(success() ~>! rep(unaryValueOperator) ~ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the purpose of |
||||||
(numericLit | stringLit | identifierName | parenthesizedValueExpression), binaryValueOperator) | ||||||
|
||||||
lazy val parenthesizedValueExpression = | ||||||
"(" ~! valueExpression ~ ")" | ||||||
|
||||||
lazy val binaryValueOperator = | ||||||
elem("binary operator", tok => binaryValueOperators.contains(tok.chars)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather you use a more direct style, like
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may I ask why? in the current version the operators occur only once instead of twice at different locations |
||||||
|
||||||
lazy val unaryValueOperator = | ||||||
elem("unary operator", tok => unaryValueOperators.contains(tok.chars)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here:
Suggested change
|
||||||
|
||||||
lazy val ambientClassDecl: Parser[DeclTree] = | ||||||
(abstractModifier <~ "class") ~ typeName ~ tparams ~ classParent ~ classImplements ~ memberBlock <~ opt(";") ^^ { | ||||||
|
@@ -342,8 +367,8 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions { | |||||
stringLit ^^ StringLiteral | ||||||
|
||||||
lazy val numberLiteral: Parser[NumberLiteral] = | ||||||
numericLit ^^ { s => | ||||||
val d = s.toDouble | ||||||
opt("-") ~ numericLit ^^ { case minus ~ s => | ||||||
val d = (minus.getOrElse("") + s).toDouble | ||||||
if (!s.contains(".") && d.isValidInt) { | ||||||
IntLiteral(d.toInt) | ||||||
} else { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure there is a (single) new line at the end of the file, so that git and GitHub don't have that mark.