Skip to content

Commit d05c665

Browse files
committed
Convert some of the fixture grammars from JSON to JS
These tests are easier to write and maintain if the grammars are just JS, like grammars normally are. It doesn't slow the tests down significantly to shell out to `node` for each of these grammars.
1 parent ddb12dc commit d05c665

File tree

43 files changed

+386
-792
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+386
-792
lines changed

cli/src/generate/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn generate_parser_for_grammar_with_opts(
157157
})
158158
}
159159

160-
fn load_grammar_file(grammar_path: &Path) -> Result<String> {
160+
pub fn load_grammar_file(grammar_path: &Path) -> Result<String> {
161161
match grammar_path.extension().and_then(|e| e.to_str()) {
162162
Some("js") => Ok(load_js_grammar_file(grammar_path)?),
163163
Some("json") => Ok(fs::read_to_string(grammar_path)?),

cli/src/tests/corpus_test.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ fn test_feature_corpus_files() {
269269
}
270270

271271
let test_path = entry.path();
272-
let grammar_path = test_path.join("grammar.json");
272+
let mut grammar_path = test_path.join("grammar.js");
273+
if !grammar_path.exists() {
274+
grammar_path = test_path.join("grammar.json");
275+
}
273276
let error_message_path = test_path.join("expected_error.txt");
274-
let grammar_json = fs::read_to_string(grammar_path).unwrap();
277+
let grammar_json = generate::load_grammar_file(&grammar_path).unwrap();
275278
let generate_result = generate::generate_parser_for_grammar(&grammar_json);
276279

277280
if error_message_path.exists() {

test/fixtures/test_grammars/aliased_inlined_rules/corpus.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
OK
33
=========================
44

5-
a.b.c
5+
a.b.c;
66

77
---
88

9-
(expression (member_expression
10-
(expression (member_expression
11-
(expression (variable_name))
9+
(statement
10+
(member_expression
11+
(member_expression
12+
(variable_name)
13+
(property_name))
1214
(property_name)))
13-
(property_name)))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This grammar shows that `ALIAS` rules can *contain* a rule that is marked as `inline`. It also
2+
// shows that you can alias a rule that would otherwise be anonymous, and it will then appear as a
3+
// named node.
4+
5+
module.exports = grammar({
6+
name: 'aliased_inlined_rules',
7+
8+
extras: $ => [/\s/],
9+
10+
inline: $ => [$.identifier],
11+
12+
rules: {
13+
statement: $ => seq($._expression, ';'),
14+
15+
_expression: $ => choice(
16+
$.member_expression,
17+
alias($.identifier, $.variable_name),
18+
),
19+
20+
member_expression: $ => prec.left(1, seq(
21+
$._expression,
22+
'.',
23+
alias($.identifier, $.property_name)
24+
)),
25+
26+
identifier: $ => choice('a', 'b', 'c')
27+
}
28+
});

test/fixtures/test_grammars/aliased_inlined_rules/grammar.json

Lines changed: 0 additions & 59 deletions
This file was deleted.

test/fixtures/test_grammars/aliased_inlined_rules/readme.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = grammar({
2+
name: 'aliased_rules',
3+
4+
extras: $ => [/\s/],
5+
6+
rules: {
7+
statement: $ => seq($._expression, ';'),
8+
9+
_expression: $ => choice(
10+
$.call_expression,
11+
$.member_expression,
12+
alias($.identifier, $.variable_name),
13+
),
14+
15+
call_expression: $ => prec.left(seq(
16+
$._expression,
17+
'(',
18+
$._expression,
19+
')'
20+
)),
21+
22+
member_expression: $ => prec.left(1, seq(
23+
$._expression,
24+
'.',
25+
alias($.identifier, $.property_name)
26+
)),
27+
28+
identifier: $ => /[a-z]+/
29+
}
30+
});

test/fixtures/test_grammars/aliased_rules/grammar.json

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This grammar shows that `ALIAS` rules can be applied directly to `TOKEN` and `IMMEDIATE_TOKEN`
2+
// rules.
3+
4+
module.exports = grammar({
5+
name: 'aliased_token_rules',
6+
7+
extras: $ => [/\s/],
8+
9+
rules: {
10+
expression: $ => seq(
11+
'a',
12+
alias(token(seq('b', 'c')), $.X),
13+
alias(token.immediate(seq('d', 'e')), $.Y),
14+
),
15+
}
16+
});

test/fixtures/test_grammars/aliased_token_rules/grammar.json

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)