Now, grammars can handle whitespace by making it another ubiquitous token, like comments. For now, this has the side effect of whitespace being included in the tree that precedes it. This was already an issue for other ubiquitous tokens though, so it needs to be fixed anyway.
142 lines
4.2 KiB
Text
142 lines
4.2 KiB
Text
==========================================
|
|
parses function calls
|
|
==========================================
|
|
x.theMethod(5, 6);
|
|
---
|
|
(program (expression_statement (function_call
|
|
(property_access (identifier) (identifier))
|
|
(number) (number))))
|
|
|
|
==========================================
|
|
parses constructor calls
|
|
==========================================
|
|
var x = new Node(5, new Node(3, null));
|
|
---
|
|
(program (var_declaration
|
|
(identifier)
|
|
(constructor_call (function_call (identifier)
|
|
(number)
|
|
(constructor_call (function_call (identifier)
|
|
(number)
|
|
(null)))))))
|
|
|
|
==========================================
|
|
parses property access with dot notation
|
|
==========================================
|
|
object.property = "the-value";
|
|
print(object.property);
|
|
---
|
|
(program
|
|
(expression_statement (assignment
|
|
(property_access (identifier) (identifier))
|
|
(string)))
|
|
(expression_statement (function_call
|
|
(identifier)
|
|
(property_access (identifier) (identifier)))))
|
|
|
|
==========================================
|
|
parses property access across lines
|
|
==========================================
|
|
object
|
|
.someProperty
|
|
.otherProperty
|
|
---
|
|
(program (expression_statement
|
|
(property_access
|
|
(property_access (identifier) (identifier))
|
|
(identifier))))
|
|
|
|
===========================================
|
|
parses dynamic property access
|
|
==========================================
|
|
object[propertName()] = propertyValue();
|
|
print(object[propertyName()]);
|
|
---
|
|
(program
|
|
(expression_statement (assignment
|
|
(property_access (identifier) (function_call (identifier)))
|
|
(function_call (identifier))))
|
|
(expression_statement (function_call
|
|
(identifier)
|
|
(property_access (identifier) (function_call (identifier))))))
|
|
|
|
==========================================
|
|
parses ternary expressions
|
|
==========================================
|
|
print(isDone() ? stuff : otherStuff);
|
|
---
|
|
(program (expression_statement
|
|
(function_call
|
|
(identifier)
|
|
(ternary (function_call (identifier)) (expression (identifier)) (identifier)))))
|
|
|
|
==========================================
|
|
parses mathematical operators
|
|
==========================================
|
|
-a + b * c - d / +e
|
|
|
|
---
|
|
(program (expression_statement
|
|
(math_op
|
|
(math_op (expression (identifier)))
|
|
(math_op
|
|
(math_op (expression (identifier)) (expression (identifier)))
|
|
(math_op (expression (identifier)) (math_op (identifier)))))))
|
|
|
|
==========================================
|
|
parses boolean operators
|
|
=========================================
|
|
!a || !(b && c)
|
|
|
|
---
|
|
(program (expression_statement
|
|
(bool_op
|
|
(bool_op (expression (identifier)))
|
|
(bool_op
|
|
(expression (bool_op (expression (identifier)) (identifier)))))))
|
|
|
|
===========================================
|
|
parses the type operators
|
|
===========================================
|
|
print((x instanceof Array) || (typeof x == "string"))
|
|
|
|
---
|
|
(program (expression_statement (function_call (identifier)
|
|
(bool_op
|
|
(expression (instanceof_expression (expression (identifier)) (identifier)))
|
|
(expression (typeof_expression (bool_op (expression (identifier)) (string))))))))
|
|
|
|
============================================
|
|
parses the 'in' operator
|
|
===========================================
|
|
print(x in y)
|
|
|
|
---
|
|
(program (expression_statement (function_call
|
|
(identifier)
|
|
(in_expression (expression (identifier)) (identifier)))))
|
|
|
|
============================================
|
|
parses assignment operators
|
|
============================================
|
|
x += 1;
|
|
x -= 1;
|
|
x *= 2;
|
|
x /= 2;
|
|
---
|
|
(program
|
|
(expression_statement (assignment (identifier) (number)))
|
|
(expression_statement (assignment (identifier) (number)))
|
|
(expression_statement (assignment (identifier) (number)))
|
|
(expression_statement (assignment (identifier) (number))))
|
|
|
|
============================================
|
|
parses property access and operators
|
|
============================================
|
|
print(x.y.z && a.b.c)
|
|
|
|
---
|
|
(program (expression_statement (function_call (identifier)
|
|
(bool_op
|
|
(property_access (property_access (identifier) (identifier)) (identifier))
|
|
(property_access (property_access (identifier) (identifier)) (identifier))))))
|