Skip to content

Commit 0c18f52

Browse files
authored
Merge pull request #145 from aspeddro/refactor-remove-extension
refactor: get rid of `$._raw_js_extension` and `$._raw_ggl_extension`
2 parents 332b518 + e6a0283 commit 0c18f52

File tree

6 files changed

+88
-96
lines changed

6 files changed

+88
-96
lines changed

grammar.js

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ module.exports = grammar({
9898
[$._record_field_name, $.record_pattern],
9999
[$.decorator],
100100
[$._statement, $._one_or_more_statements],
101-
[$._simple_extension],
102101
[$._inline_type, $.function_type_parameters],
103102
[$.primary_expression, $.parameter, $._pattern],
104103
[$.parameter, $._pattern],
@@ -1153,65 +1152,13 @@ module.exports = grammar({
11531152
))
11541153
)),
11551154

1156-
extension_expression: $ => prec('call', seq(
1155+
extension_expression: $ => prec.right(seq(
11571156
repeat1('%'),
1158-
choice(
1159-
$._raw_js_extension,
1160-
$._raw_gql_extension,
1161-
$._simple_extension,
1162-
),
1163-
)),
1164-
1165-
_simple_extension: $ => seq(
11661157
$.extension_identifier,
1167-
optional($._extension_expression_payload),
1168-
),
1169-
1170-
_raw_js_extension: $ => seq(
1171-
alias(token('raw'), $.extension_identifier),
1172-
'(',
1173-
alias($._raw_js, $.expression_statement),
1174-
')',
1175-
),
1176-
1177-
_raw_js: $ => choice(
1178-
alias($._raw_js_template_string, $.template_string),
1179-
alias($._raw_js_string, $.string),
1180-
),
1181-
1182-
_raw_js_string: $ => alias($.string, $.raw_js),
1183-
1184-
_raw_js_template_string: $ => seq(
1185-
token(seq(
1186-
optional(choice(
1187-
'j',
1188-
'js',
1189-
)),
1190-
'`',
1191-
)),
1192-
alias(repeat($._template_string_content), $.raw_js),
1193-
'`',
1194-
),
1195-
1196-
_raw_gql_extension: $ => seq(
1197-
alias(token('graphql'), $.extension_identifier),
1198-
'(',
1199-
alias($._raw_gql, $.expression_statement),
1200-
')',
1201-
),
1202-
1203-
_raw_gql: $ => choice(
1204-
alias($._raw_gql_template_string, $.template_string),
1205-
alias($._raw_gql_string, $.string),
1206-
),
1207-
1208-
_raw_gql_string: $ => alias($.string, $.raw_gql),
1209-
1210-
_raw_gql_template_string: $ => seq(
1211-
'`',
1212-
alias(repeat($._template_string_content), $.raw_gql),
1213-
'`',
1214-
),
1158+
optional(
1159+
$._extension_expression_payload,
1160+
)
1161+
)),
12151162

12161163
_extension_expression_payload: $ => seq(
12171164
'(',
@@ -1460,18 +1407,21 @@ module.exports = grammar({
14601407
)),
14611408
'`',
14621409
)),
1463-
repeat($._template_string_content),
1410+
$.template_string_content,
14641411
'`'
14651412
),
14661413

1467-
_template_string_content: $ => choice(
1468-
$._template_chars,
1469-
$.template_substitution,
1470-
choice(
1471-
alias('\\`', $.escape_sequence),
1472-
$.escape_sequence,
1414+
template_string_content: $ =>
1415+
repeat1(
1416+
choice(
1417+
$._template_chars,
1418+
$.template_substitution,
1419+
choice(
1420+
alias('\\`', $.escape_sequence),
1421+
$.escape_sequence,
1422+
)
1423+
),
14731424
),
1474-
),
14751425

14761426
template_substitution: $ => choice(
14771427
seq('$', $.value_identifier),

queries/injections.scm

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
(raw_js) @javascript
2-
(raw_gql) @graphql
31
(comment) @comment
2+
3+
; %re
4+
(extension_expression
5+
(extension_identifier) @_name
6+
(#eq? @_name "re")
7+
(expression_statement (_) @regex))
8+
9+
; %raw
10+
(extension_expression
11+
(extension_identifier) @_name
12+
(#eq? @_name "raw")
13+
(expression_statement
14+
(_ (_) @javascript)))
15+
16+
; %graphql
17+
(extension_expression
18+
(extension_identifier) @_name
19+
(#eq? @_name "graphql")
20+
(expression_statement
21+
(_ (_) @graphql)))

test/corpus/expressions.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,7 @@ module Test = %graphql(`
11181118
(extension_identifier)
11191119
(expression_statement
11201120
(string
1121-
(raw_js
1122-
(string_fragment))))))
1121+
(string_fragment)))))
11231122
(expression_statement
11241123
(extension_expression
11251124
(extension_identifier)
@@ -1148,7 +1147,7 @@ module Test = %graphql(`
11481147
(extension_identifier)
11491148
(expression_statement
11501149
(template_string
1151-
(raw_js
1150+
(template_string_content
11521151
(escape_sequence)
11531152
(escape_sequence))))))
11541153
(let_binding
@@ -1157,7 +1156,7 @@ module Test = %graphql(`
11571156
(extension_identifier)
11581157
(expression_statement
11591158
(template_string
1160-
(raw_js
1159+
(template_string_content
11611160
(escape_sequence)
11621161
(escape_sequence))))))
11631162
(module_declaration
@@ -1166,7 +1165,7 @@ module Test = %graphql(`
11661165
(extension_identifier)
11671166
(expression_statement
11681167
(template_string
1169-
(raw_gql))))))
1168+
(template_string_content))))))
11701169

11711170
===========================================
11721171
Raise expression

test/corpus/literals.txt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,30 @@ The caller should either handle this error, or expect that exit code.`
133133
----
134134

135135
(source_file
136-
(expression_statement (template_string))
137-
(expression_statement (template_string))
138-
(expression_statement (template_string))
139-
(expression_statement (template_string
140-
(template_substitution
141-
(binary_expression (number) (number)))
142-
(template_substitution
143-
(binary_expression (number) (number)))))
144-
(expression_statement (template_string
145-
(escape_sequence)
146-
(template_substitution (call_expression
147-
(value_identifier)
148-
(arguments (string (string_fragment)))))
149-
(escape_sequence)
150-
(template_substitution (value_identifier))))
151-
(expression_statement (template_string))
152-
(expression_statement (template_string (escape_sequence))))
136+
(expression_statement (template_string (template_string_content)))
137+
(expression_statement (template_string (template_string_content)))
138+
(expression_statement (template_string (template_string_content)))
139+
(expression_statement
140+
(template_string
141+
(template_string_content
142+
(template_substitution
143+
(binary_expression (number) (number)))
144+
(template_substitution
145+
(binary_expression (number) (number))))))
146+
(expression_statement
147+
(template_string
148+
(template_string_content
149+
(escape_sequence)
150+
(template_substitution
151+
(call_expression
152+
(value_identifier)
153+
(arguments
154+
(string
155+
(string_fragment)))))
156+
(escape_sequence)
157+
(template_substitution (value_identifier)))))
158+
(expression_statement (template_string (template_string_content)))
159+
(expression_statement (template_string (template_string_content (escape_sequence)))))
153160

154161
============================================
155162
Tricky template strings
@@ -164,11 +171,11 @@ Tricky template strings
164171
---
165172

166173
(source_file
167-
(expression_statement (template_string))
168-
(expression_statement (template_string))
169-
(expression_statement (template_string))
170-
(expression_statement (template_string))
171-
(expression_statement (template_string)))
174+
(expression_statement (template_string (template_string_content)))
175+
(expression_statement (template_string (template_string_content)))
176+
(expression_statement (template_string (template_string_content)))
177+
(expression_statement (template_string (template_string_content)))
178+
(expression_statement (template_string (template_string_content))))
172179

173180
============================================
174181
Characters

test/corpus/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ external add: (
382382
(parameter
383383
(type_identifier_path (module_identifier) (type_identifier)))
384384
(parameter
385-
(decorator (decorator_identifier) (decorator_arguments (template_string)))
385+
(decorator (decorator_identifier) (decorator_arguments (template_string (template_string_content))))
386386
(type_identifier))
387387
(parameter
388388
(decorator (decorator_identifier) (decorator_arguments (string)))

test/manual/embedded.res

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// NOT AN AUTOMATED TEST.
2+
//
3+
// Looks like Tree-sitter test framework does not allow to test
4+
// for correct language embedding. So, this file is just a showcase
5+
// to observe results with eyes.
6+
//
7+
// You should see comprehensive highlighting for constructs inside
8+
// strings. That is, they should not look like plain strings if
9+
// you have corresponding grammar installed.
10+
11+
// :TSInstall javascript
12+
let inc = %raw(`function(x) {return x + 1;}`)
13+
14+
// :TSInstall graphql
15+
let gql = %graphql(`{ hero { name } }`)
16+
17+
// :TSInstall regex
18+
let re = %re(`^[A-Z][a-z0-9]*$`)

0 commit comments

Comments
 (0)