Remove repetition from language spec descriptions

This commit is contained in:
Max Brunsfeld 2014-10-19 12:36:43 -07:00
parent 38ad248a53
commit 8134b64d00
14 changed files with 250 additions and 71 deletions

View file

@ -1,15 +1,21 @@
=====================================================
recovers from errors at the top level
errors at the top level
=====================================================
x * * y
---
(variable) (ERROR '*')
=====================================================
recovers from errors inside parenthesized expressions
errors inside parenthesized expressions
=====================================================
x + (y * + z) * 5
---
(sum
(variable)
(product (group (ERROR '+')) (number)))

View file

@ -1,52 +1,73 @@
====================
parses numbers
numbers
===================
5
---
(number)
===================
parses variables
variables
===================
x
---
(variable)
====================================
parses variables with greek letters
variables with greek letters
====================================
φ1
---
(variable)
===================
parses products
products
===================
x * x
---
(product (variable) (variable))
===================
parses sums
sums
===================
x + x
---
(sum (variable) (variable))
===============================================
binds multiplication more tightly than addition
operators of different precedence
===============================================
a * b + c * d
---
(sum
(product (variable) (variable))
(product (variable) (variable)))
============================
parses exponents
exponents
============================
x + y * z^(a + b)
---
(sum
(variable)
(product

View file

@ -1,12 +1,15 @@
==========================================
parses simple declarations
simple declarations
==========================================
package trivial
type x int64
var y = ""
func z() {}
---
(program
(package_directive (package_name))
(type_declaration (type_name) (type_name))
@ -14,21 +17,25 @@ func z() {}
(func_declaration (var_name) (block_statement)))
==========================================
parses var declarations
var declarations
==========================================
package trivial
var X struct { Y int64 }
var Z = ""
---
(program
(package_directive (package_name))
(var_declaration (var_name) (struct_type (var_name) (type_name)))
(var_declaration (var_name) (string)))
==========================================
parses comments
comments
==========================================
package trivial
var x = 1 // on variable
@ -36,8 +43,10 @@ var x = 1 // on variable
func main() {
// in function
}
---
(program
(package_directive (package_name))
(var_declaration (var_name) (expression (number) (comment)))
(func_declaration (var_name) (block_statement (comment))))
(func_declaration (var_name) (block_statement (comment))))

View file

@ -1,11 +1,14 @@
==========================================
handles indented code after blocks
indented code after blocks
=========================================
package trivial
func one() {}
func two() {}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (block_statement))

View file

@ -1,13 +1,16 @@
==========================================
parses function calls
function calls
==========================================
package main
func main() {
println("1", 2)
println()
}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (block_statement
@ -15,14 +18,17 @@ func main() {
(expression_statement (call_expression (var_name))))))
============================================
parses selector expressions
selector expressions
============================================
package main
func main() {
x.SomeMethod(x.SomeField, x.OtherField.NestedField);
}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (block_statement

View file

@ -1,5 +1,5 @@
============================================
parses return statements
return statements
============================================
package main
@ -10,7 +10,9 @@ func main() {
func helper() {
return 1, two, "three"
}
---
(program (package_directive (package_name))
(func_declaration (var_name) (block_statement
(return_statement)))
@ -18,8 +20,9 @@ func helper() {
(return_statement (number) (var_name) (string)))))
============================================
parses variable declaration statements
variable declarations
============================================
package main
func main() {
@ -27,7 +30,9 @@ func main() {
var z = 10
println(x, y, z)
}
---
(program (package_directive (package_name))
(func_declaration (var_name) (block_statement
(declaration_statement (var_name) (var_name) (call_expression (var_name)))
@ -35,8 +40,9 @@ func main() {
(expression_statement (call_expression (var_name) (var_name) (var_name) (var_name))))))
============================================
parses if statements
if statements
============================================
package main
func main() {
@ -51,7 +57,9 @@ func main() {
} else if condition4() {
}
}
---
(program (package_directive (package_name))
(func_declaration (var_name) (block_statement
(if_statement (call_expression (var_name))
@ -64,8 +72,9 @@ func main() {
(block_statement))))))
=============================================
parses range statements
range statements
=============================================
package main
func main() {
@ -77,10 +86,12 @@ func main() {
println(k, v)
}
}
---
(program (package_directive (package_name))
(func_declaration (var_name) (block_statement
(range_statement (var_name) (call_expression (var_name)) (block_statement
(expression_statement (call_expression (var_name) (var_name)))))
(range_statement (var_name) (var_name) (call_expression (var_name)) (block_statement
(expression_statement (call_expression (var_name) (var_name) (var_name))))))))
(expression_statement (call_expression (var_name) (var_name) (var_name))))))))

View file

@ -1,6 +1,7 @@
==========================================
parses complex types
composite types
==========================================
package main
type x *struct {
@ -9,7 +10,9 @@ type x *struct {
DoStuff()
}
}
---
(program
(package_directive (package_name))
(type_declaration
@ -19,14 +22,17 @@ type x *struct {
(var_name) (map_type (type_name) (interface_type (var_name)))))))
============================================
parses functions arguments
functions arguments
============================================
package main
func oneArg(arg1 interface{}) {}
func argsOfSameType(arg1, arg2 string) {}
func argsOfDifferentTypes() (arg1 string, arg2 int64) {}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (var_name) (interface_type) (block_statement))
@ -34,13 +40,16 @@ func argsOfDifferentTypes() (arg1 string, arg2 int64) {}
(func_declaration (var_name) (var_name) (type_name) (var_name) (type_name) (block_statement)))
============================================
parses functions with unnamed return values
functions with unnamed return values
============================================
package main
func oneReturnValue() string {}
func multipleReturnValues() (string, int64, error) {}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (type_name)
@ -49,13 +58,16 @@ func multipleReturnValues() (string, int64, error) {}
(block_statement)))
============================================
parses functions with named return values
functions with named return values
============================================
package main
func oneReturnValue() (result string) {}
func multipleReturnValues() (result string, count int64, err error) {}
---
(program
(package_directive (package_name))
(func_declaration (var_name) (var_name) (type_name)

View file

@ -1,10 +1,13 @@
==========================================
parses multiple statements
multiple statements
==========================================
var x = {}, z, i = 0;
firstFunction(x)
secondFunction(x);
---
(program
(var_declaration
(var_assignment (identifier) (object))
@ -14,32 +17,39 @@ secondFunction(x);
(expression_statement (function_call (identifier) (identifier))))
==========================================
js parses if statements
if statements
==========================================
if (isReady()) {
console.log(theData)
}
---
(if_statement (function_call (identifier))
(statement_block (expression_statement (function_call (property_access (identifier) (identifier)) (identifier)))))
==========================================
parses if-else statements
if-else statements
==========================================
if (theCondition) {
firstFunction();
} else {
secondFunction();
}
---
(if_statement
(identifier)
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier)))))
==================================================
parses if-else statements with multiple conditions
if-else statements with multiple conditions
==================================================
if (firstValue) {
firstFunction();
} else if (secondValue)
@ -47,7 +57,9 @@ if (firstValue) {
else {
thirdFunction();
}
---
(if_statement (identifier)
(statement_block (expression_statement (function_call (identifier))))
(if_statement (identifier)
@ -55,12 +67,15 @@ else {
(statement_block (expression_statement (function_call (identifier))))))
==========================================
parses for loops
for loops
==========================================
for (var i = 1; someCondition(i); i = next()) {
doSomething();
}
---
(for_statement
(var_declaration (var_assignment (identifier) (number)))
(expression_statement (function_call (identifier) (identifier)))
@ -68,13 +83,16 @@ for (var i = 1; someCondition(i); i = next()) {
(statement_block (expression_statement (function_call (identifier)))))
==========================================
parses for-in loops
for-in loops
==========================================
for (var key in someObject)
doSomething();
for (key in someObject)
doSomethingElse();
---
(program
(for_in_statement
(identifier) (identifier)
@ -84,19 +102,22 @@ for (key in someObject)
(expression_statement (function_call (identifier)))))
==========================================
parses while loops
while loops
==========================================
while (someCondition(i)) {
doSomething();
}
---
(while_statement
(function_call (identifier) (identifier))
(statement_block (expression_statement (function_call (identifier)))))
==========================================
parses try/catch statements
try/catch statements
==========================================
try {
doSomething();
} catch (e) {
@ -108,7 +129,9 @@ try {
} finally {
logError();
}
---
(program
(try_statement
(statement_block (expression_statement (function_call (identifier))))
@ -120,26 +143,33 @@ try {
(statement_block (expression_statement (function_call (identifier)))))))
===========================================
parses throw statements
throw statements
===========================================
throw new Error("wtf");
---
(throw_statement (constructor_call (function_call (identifier) (string))))
===========================================
parses indented code after blocks
indented code after blocks
===========================================
function x() {}
return z;
---
(program
(expression_statement
(function_expression (identifier) (formal_parameters) (statement_block)))
(return_statement (identifier)))
===========================================
parses switch statements
switch statements
===========================================
switch(x) {
case "hello":
print("one");
@ -150,7 +180,9 @@ switch(x) {
default:
print("three");
}
---
(switch_statement (identifier)
(switch_case
(string)

View file

@ -1,19 +1,25 @@
==========================================
recovers from errors in function calls
errors in function calls
==========================================
stuff(|||);
---
(expression_statement (function_call (identifier) (ERROR '|')))
==========================================
recovers from errors in if statements
errors in if statements
==========================================
stuff();
if (*nonsense*) {
*more-nonsense*;
}
moreStuff();
---
(program
(expression_statement (function_call (identifier)))
(if_statement (ERROR '*')

View file

@ -1,36 +1,48 @@
==========================================
parses regexes
regexes
==========================================
theFunction(/regex1/, /regex2/g);
---
(expression_statement (function_call (identifier)
(regex) (regex)))
==========================================
parses numbers
numbers
==========================================
theFunction(100.0, 200);
---
(expression_statement (function_call (identifier)
(number) (number)))
==========================================
parses strings
strings
==========================================
theFunction('', "", 'single-quoted-string', "double-quoted-string");
---
(expression_statement (function_call (identifier)
(string) (string) (string) (string)))
==========================================
parses function expressions
function expressions
==========================================
var x = {
theMethod: function(argA, argB) {
var x = argA;
}
};
---
(var_declaration (var_assignment
(identifier)
(object (identifier) (function_expression
@ -39,8 +51,9 @@ var x = {
(var_declaration (var_assignment (identifier) (identifier))))))))
==========================================
parses comments
comments
==========================================
// this is the beginning of the script.
// here we go.
var thing = {
@ -53,6 +66,7 @@ var thing = {
doStuff();
}
};
---
(comment)
(comment)
@ -68,9 +82,12 @@ var thing = {
(expression_statement (function_call (identifier))))))))
==========================================
parses comments within expressions
comments within expressions
==========================================
y // comment
* z;
---
(expression_statement (math_op (expression (identifier) (comment)) (identifier)))

View file

@ -1,17 +1,23 @@
==========================================
parses function calls
function calls
==========================================
x.theMethod(5, 6);
---
(expression_statement (function_call
(property_access (identifier) (identifier))
(number) (number)))
==========================================
parses constructor calls
constructor calls
==========================================
var x = new Node(5, new Node(3, null));
---
(var_declaration (var_assignment
(identifier)
(constructor_call (function_call (identifier)
@ -21,11 +27,14 @@ var x = new Node(5, new Node(3, null));
(null)))))))
==========================================
parses property access with dot notation
property access with dot notation
==========================================
object.property = "the-value";
print(object.property);
---
(program
(expression_statement (assignment
(property_access (identifier) (identifier))
@ -35,22 +44,27 @@ print(object.property);
(property_access (identifier) (identifier)))))
==========================================
parses property access across lines
property access across lines
==========================================
object
.someProperty
.otherProperty
---
(expression_statement
(property_access
(property_access (identifier) (identifier))
(identifier)))
===========================================
parses dynamic property access
dynamic property access
==========================================
object[propertName()] = propertyValue();
print(object[propertyName()]);
---
(program
(expression_statement (assignment
@ -61,18 +75,22 @@ print(object[propertyName()]);
(property_access (identifier) (function_call (identifier))))))
==========================================
parses ternary expressions
ternary expressions
==========================================
print(isDone() ? stuff : otherStuff);
---
(expression_statement
(function_call
(identifier)
(ternary (function_call (identifier)) (identifier) (identifier))))
==========================================
parses mathematical operators
mathematical operators
==========================================
-a + b * c - d / +e
---
@ -84,11 +102,13 @@ parses mathematical operators
(math_op (identifier) (math_op (identifier))))))
==========================================
parses boolean operators
boolean operators
=========================================
!a || !(b && c)
---
(expression_statement
(bool_op
(bool_op (identifier))
@ -96,19 +116,22 @@ parses boolean operators
(expression (bool_op (identifier) (identifier))))))
===========================================
parses the type operators
type operators
===========================================
print((x instanceof Array) || (typeof x == "string"))
---
(expression_statement (function_call (identifier)
(bool_op
(expression (instanceof_expression (identifier) (identifier)))
(expression (typeof_expression (bool_op (identifier) (string)))))))
============================================
parses the 'in' operator
the 'in' operator
===========================================
print(x in y)
---
@ -117,13 +140,16 @@ print(x in y)
(in_expression (identifier) (identifier))))
============================================
parses assignment operators
assignment operators
============================================
x += 1;
x -= 1;
x *= 2;
x /= 2;
---
(program
(expression_statement (assignment (identifier) (number)))
(expression_statement (assignment (identifier) (number)))
@ -131,11 +157,13 @@ x /= 2;
(expression_statement (assignment (identifier) (number))))
============================================
parses property access and operators
property access and operators
============================================
print(x.y.z && a.b.c)
---
(expression_statement (function_call (identifier)
(bool_op
(property_access (property_access (identifier) (identifier)) (identifier))

View file

@ -1,21 +1,29 @@
==========================================
recovers from top-level errors
top-level errors
==========================================
[}
---
(ERROR '}')
==========================================
recovers from unexpected tokens
unexpected tokens
==========================================
barf
---
(ERROR 'b')
==========================================
recovers from errors inside arrays
errors inside arrays
==========================================
[1, , 2]
---
(array
(number)
@ -23,17 +31,23 @@ recovers from errors inside arrays
(number))
==========================================
recovers from errors inside objects
errors inside objects
==========================================
{ "key1": 1, oops }
---
(object (string) (number) (ERROR 'o'))
==========================================
recovers from errors inside nested objects
errors inside nested objects
==========================================
{ "key1": { "key2": 1, 2 }, [, "key3": 3 }
---
(object
(string) (object (string) (number) (ERROR '2'))
(ERROR '[')

View file

@ -1,20 +1,27 @@
=============================
parses floating point numbers
floating point numbers
=============================
3.14
---
(number)
===================
parses empty arrays
empty arrays
===================
[]
---
(array)
===================
parses arrays
arrays
===================
[
333,
null,
@ -22,7 +29,9 @@ parses arrays
false,
{ "stuff": "good" }
]
---
(array
(number)
(null)
@ -31,21 +40,26 @@ parses arrays
(object (string) (string)))
====================
parses empty objects
empty objects
====================
{}
---
(object)
===================
parses long objects
long objects
===================
{
"key1": "value1",
"key2": 1
}
---
(object
(string) (string)
(string) (number))

View file

@ -26,11 +26,11 @@ describe("Languages", [&]() {
});
for (auto &entry : test_entries_for_language(language_name)) {
it(entry.description.c_str(), [&]() {
it(("parses " + entry.description).c_str(), [&]() {
ts_document_set_input_string(doc, entry.input.c_str());
auto doc_string = ts_node_string(ts_document_root_node(doc));
AssertThat(doc_string, Equals(("(DOCUMENT " + entry.tree_string + ")").c_str()));
free((void *)doc_string);
const char *node_string = ts_node_string(ts_document_root_node(doc));
AssertThat(node_string, Equals(("(DOCUMENT " + entry.tree_string + ")").c_str()));
free((void *)node_string);
});
}
});