Skip to content

Commit

Permalink
Make string content visible
Browse files Browse the repository at this point in the history
All types of content are aliased as content. Query patterns will usually
need to include the parent rule anyways.

Since the alias only includes content tokenized by the scanner, escape
sequences are _not_ included.

Co-authored-by: MichaelHatherly <michaelhatherly@gmail.com>
  • Loading branch information
savq and MichaelHatherly committed Nov 5, 2024
1 parent 78eea43 commit 73e9669
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
16 changes: 8 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,25 +1024,25 @@ module.exports = grammar({
string_literal: $ => choice(
seq(
$._delimiter_str_1,
repeat(choice($._content_str_1, $.string_interpolation, $.escape_sequence)),
repeat(choice(alias($._content_str_1, $.content), $.string_interpolation, $.escape_sequence)),
$._end_str,
),
seq(
$._delimiter_str_3,
repeat(choice($._content_str_3, $.string_interpolation, $.escape_sequence)),
repeat(choice(alias($._content_str_3, $.content), $.string_interpolation, $.escape_sequence)),
$._end_str,
),
),

command_literal: $ => choice(
seq(
$._delimiter_cmd_1,
repeat(choice($._content_cmd_1, $.string_interpolation, $.escape_sequence)),
repeat(choice(alias($._content_cmd_1, $.content), $.string_interpolation, $.escape_sequence)),
$._end_cmd,
),
seq(
$._delimiter_cmd_3,
repeat(choice($._content_cmd_3, $.string_interpolation, $.escape_sequence)),
repeat(choice(alias($._content_cmd_3, $.content), $.string_interpolation, $.escape_sequence)),
$._end_cmd,
),
),
Expand All @@ -1053,12 +1053,12 @@ module.exports = grammar({
choice(
seq(
$._delimiter_str_1,
repeat(choice($._content_str_1_raw, $.escape_sequence)),
repeat(choice(alias($._content_str_1_raw, $.content), $.escape_sequence)),
$._end_str,
),
seq(
$._delimiter_str_3,
repeat(choice($._content_str_3_raw, $.escape_sequence)),
repeat(choice(alias($._content_str_3_raw, $.content), $.escape_sequence)),
$._end_str,
),
),
Expand All @@ -1071,12 +1071,12 @@ module.exports = grammar({
choice(
seq(
$._delimiter_cmd_1,
repeat(choice($._content_cmd_1_raw, $.escape_sequence)),
repeat(choice(alias($._content_cmd_1_raw, $.content), $.escape_sequence)),
$._end_cmd,
),
seq(
$._delimiter_cmd_3,
repeat(choice($._content_cmd_3_raw, $.escape_sequence)),
repeat(choice(alias($._content_cmd_3_raw, $.content), $.escape_sequence)),
$._end_cmd,
),
),
Expand Down
4 changes: 3 additions & 1 deletion test/corpus/definitions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ Base.show(io::IO, ::MIME"text/plain", m::Method; kwargs...) = show_method(io, m,
(argument_list
(typed_expression (identifier) (identifier))
(unary_typed_expression
(prefixed_string_literal prefix: (identifier)))
(prefixed_string_literal
prefix: (identifier)
(content)))
(typed_expression (identifier) (identifier))
(splat_expression (identifier))))
(operator)
Expand Down
16 changes: 8 additions & 8 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ df."a"

(field_expression
(identifier)
(string_literal)))
(string_literal (content))))


==============================
Expand All @@ -102,7 +102,7 @@ a[1, :]
(integer_literal)
(operator)))
(index_expression
(string_literal)
(string_literal (content))
(vector_expression (integer_literal))))


Expand Down Expand Up @@ -155,7 +155,7 @@ new{typeof(xs)}(xs)

(source_file
(call_expression (identifier) (argument_list))
(call_expression (identifier) (argument_list (string_literal) (integer_literal)))
(call_expression (identifier) (argument_list (string_literal (content)) (integer_literal)))
(call_expression (identifier) (argument_list (splat_expression (identifier))))

(call_expression
Expand Down Expand Up @@ -224,12 +224,12 @@ Meta.@dump x = 1
(macro_identifier (identifier))
(macro_argument_list
(binary_expression (identifier) (operator) (identifier))
(string_literal)))
(string_literal (content))))

(macrocall_expression
(macro_identifier (identifier))
(macro_argument_list
(string_literal)
(string_literal (content))
(compound_statement (assignment (identifier) (operator) (identifier)))))

(macrocall_expression
Expand All @@ -245,7 +245,7 @@ Meta.@dump x = 1
(identifier)
(argument_list
(macrocall_expression (macro_identifier (identifier)))
(string_literal)))
(string_literal (content))))

; Nested macros
(macrocall_expression
Expand Down Expand Up @@ -397,7 +397,7 @@ end
(vector_expression
(integer_literal)
(integer_literal)))
(interpolation_expression (string_literal))
(interpolation_expression (string_literal (content)))
(using_statement
(selected_import
(interpolation_expression (identifier))
Expand Down Expand Up @@ -482,7 +482,7 @@ x'x
(field_expression (identifier) (identifier)))
(juxtaposition_expression
(integer_literal)
(prefixed_string_literal (identifier)))
(prefixed_string_literal (identifier) (content)))
(juxtaposition_expression
(adjoint_expression (identifier))
(identifier))
Expand Down
40 changes: 26 additions & 14 deletions test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,26 @@ band = "Interpol"
(source_file
(string_literal)
(string_literal (escape_sequence))
(string_literal)
(string_literal (escape_sequence) (escape_sequence))
(string_literal)
(string_literal (content))
(string_literal (content) (escape_sequence) (content) (escape_sequence) (content))
(string_literal (content))
(assignment
(identifier)
(operator)
(string_literal))
(string_literal (content)))
(string_literal
(string_interpolation (identifier)))
(string_interpolation (identifier))
(content))
(string_literal
(string_interpolation (juxtaposition_expression (integer_literal) (identifier))))
(string_interpolation (juxtaposition_expression (integer_literal) (identifier)))
(content))
(string_literal
(content)
(string_interpolation
(string_literal
(content)
(string_interpolation
(string_literal))))))
(string_literal (content)))))))


==============================
Expand All @@ -143,38 +147,46 @@ echo "\033[31mred\033[m"
---

(source_file
(command_literal)
(command_literal (content))
(prefixed_command_literal
prefix: (identifier))
(command_literal (string_interpolation (identifier)))
(command_literal (escape_sequence) (escape_sequence))
prefix: (identifier)
(content))
(command_literal (content) (string_interpolation (identifier)))
(command_literal (content) (escape_sequence) (content) (escape_sequence))
(command_literal
(content)
(escape_sequence)
(escape_sequence)))
(content)
(escape_sequence)
(content)))


==============================
non-standard string literals
==============================

# FIXME: \s shouldn't be an escape_sequence here
trailing_ws = r"\s+$"
version = v"1.0"
K"\\"

---

(source_file
(line_comment)
(assignment
(identifier)
(operator)
(prefixed_string_literal
prefix: (identifier)
(escape_sequence)))
(escape_sequence)
(content)))
(assignment
(identifier)
(operator)
(prefixed_string_literal
prefix: (identifier)))
prefix: (identifier)
(content)))
(prefixed_string_literal
prefix: (identifier)
(escape_sequence)))
Expand Down

0 comments on commit 73e9669

Please sign in to comment.