Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nodes that mark the content of string literals #155

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,32 +1041,36 @@ module.exports = grammar({

string_literal: $ => seq(
$._string_start,
repeat(choice($._string_content, $.string_interpolation, $.escape_sequence)),
optional($.string_content),
$._string_end,
),

command_literal: $ => seq(
$._command_start,
repeat(choice($._string_content, $.string_interpolation, $.escape_sequence)),
optional($.string_content),
$._command_end,
),

string_content: $ => repeat1(choice($._string_content, $.string_interpolation, $.escape_sequence)),

prefixed_string_literal: $ => prec.left(seq(
field('prefix', $.identifier),
$._immediate_string_start,
repeat(choice($._string_content_no_interp, $.escape_sequence)),
optional($.raw_string_content),
$._string_end,
optional(field('suffix', $.identifier)),
)),

prefixed_command_literal: $ => prec.left(seq(
field('prefix', $.identifier),
$._immediate_command_start,
repeat(choice($._string_content_no_interp, $.escape_sequence)),
optional($.raw_string_content),
$._command_end,
optional(field('suffix', $.identifier)),
)),

raw_string_content: $ => repeat1(choice($._string_content_no_interp, $.escape_sequence)),

string_interpolation: $ => seq(
'$',
choice(
Expand Down
2 changes: 1 addition & 1 deletion test/corpus/definitions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ 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) (raw_string_content)))
(typed_expression (identifier) (identifier))
(splat_expression (identifier))))
(operator)
Expand Down
19 changes: 10 additions & 9 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ df."a"

(field_expression
(identifier)
(string_literal)))

(string_literal (string_content))))

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


Expand Down Expand Up @@ -155,7 +154,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 (string_content)) (integer_literal)))
(call_expression (identifier) (argument_list (splat_expression (identifier))))

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

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

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

; Nested macros
(macrocall_expression
Expand Down Expand Up @@ -397,7 +396,7 @@ end
(vector_expression
(integer_literal)
(integer_literal)))
(interpolation_expression (string_literal))
(interpolation_expression (string_literal (string_content)))
(import_statement
(selected_import
(interpolation_expression (identifier))
Expand Down Expand Up @@ -481,7 +480,9 @@ x'x
(field_expression (identifier) (identifier)))
(juxtaposition_expression
(integer_literal)
(prefixed_string_literal (identifier)))
(prefixed_string_literal
(identifier)
(raw_string_content)))
(juxtaposition_expression
(adjoint_expression (identifier))
(identifier))
Expand Down
74 changes: 50 additions & 24 deletions test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,40 @@ band = "Interpol"

(source_file
(string_literal)
(string_literal (escape_sequence))
(string_literal)
(string_literal (escape_sequence) (escape_sequence))
(string_literal)
(string_literal
(string_content
(escape_sequence)))
(string_literal
(string_content))
(string_literal
(string_content
(escape_sequence)
(escape_sequence)))
(string_literal
(string_content))
(assignment
(identifier)
(operator)
(string_literal))
(string_literal
(string_content)))
(string_literal
(string_interpolation (identifier)))
(string_content
(string_interpolation
(identifier))))
(string_literal
(string_interpolation (juxtaposition_expression (integer_literal) (identifier))))
(string_content
(string_interpolation
(juxtaposition_expression
(integer_literal)
(identifier)))))
(string_literal
(string_interpolation
(string_literal
(string_interpolation
(string_literal))))))

(string_content
(string_interpolation
(string_literal
(string_content
(string_interpolation
(string_literal
(string_content)))))))))

==============================
command string literals
Expand All @@ -143,15 +159,23 @@ echo "\033[31mred\033[m"
---

(source_file
(command_literal)
(command_literal
(string_content))
(prefixed_command_literal
prefix: (identifier))
(command_literal (string_interpolation (identifier)))
(command_literal (escape_sequence) (escape_sequence))
prefix: (identifier)
(raw_string_content))
(command_literal
(escape_sequence)
(escape_sequence)))

(string_content
(string_interpolation
(identifier))))
(command_literal
(string_content
(escape_sequence)
(escape_sequence)))
(command_literal
(string_content
(escape_sequence)
(escape_sequence))))

==============================
non-standard string literals
Expand All @@ -168,17 +192,19 @@ K"\\"
(identifier)
(operator)
(prefixed_string_literal
prefix: (identifier)
(escape_sequence)))
prefix: (identifier)
(raw_string_content
(escape_sequence))))
(assignment
(identifier)
(operator)
(prefixed_string_literal
prefix: (identifier)))
prefix: (identifier)
(raw_string_content)))
(prefixed_string_literal
prefix: (identifier)
(escape_sequence)))

(raw_string_content
(escape_sequence))))

==============================
comments
Expand Down
Loading