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

Improve parsing of numbers and C-style for loops #55

Closed
wants to merge 6 commits into from
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
6 changes: 3 additions & 3 deletions corpus/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exit 1
---

(program
(command (command_name (word)) (word)))
(command (command_name (word)) (number)))

===================================
Commands with environment variables
Expand All @@ -57,7 +57,7 @@ VAR1=a VAR2="ok" git diff --word-diff=color

(program
(command
(variable_assignment (variable_name) (word))
(variable_assignment (variable_name) (number))
(command_name (word)))
(command
(variable_assignment (variable_name) (word))
Expand Down Expand Up @@ -97,7 +97,7 @@ cat a b > /dev/null
(command (command_name (word)) (word) (word))
(file_redirect (word)))
(command
(file_redirect (file_descriptor) (word))
(file_redirect (file_descriptor) (number))
(command_name (word))))

===============================
Expand Down
23 changes: 20 additions & 3 deletions corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ F="${G%% *}"
(program
(variable_assignment
(variable_name)
(string (expansion (subscript (variable_name) (word)))))
(string (expansion (subscript (variable_name) (number)))))
(variable_assignment
(variable_name)
(string (expansion (variable_name) (regex))))
Expand Down Expand Up @@ -326,12 +326,12 @@ a+=(foo "bar" $(baz))

(program
(variable_assignment (variable_name) (array))
(variable_assignment (variable_name) (array (word) (word) (word)))
(variable_assignment (variable_name) (array (number) (number) (number)))
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
(variable_assignment
(subscript (variable_name) (simple_expansion (variable_name)))
(word))
(number))
(variable_assignment
(variable_name)
(array
Expand All @@ -351,3 +351,20 @@ echo -ne "\033k$1\033\\" > /dev/stderr
(redirected_statement
(command (command_name (word)) (word) (string (simple_expansion (variable_name))))
(file_redirect (word))))

==============================
Numbers
==============================

decimal=100
octal=032
hexadecimal=0x32
custom=64#2Az9@_1 # Custom base

---

(program
(variable_assignment (variable_name) (number))
(variable_assignment (variable_name) (number))
(variable_assignment (variable_name) (number))
(variable_assignment (variable_name) (number)) (comment))
2 changes: 1 addition & 1 deletion corpus/programs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ echo 1 \ 2 \ 3

---

(program (command (command_name (word)) (word) (word) (word)))
(program (command (command_name (word)) (number) (number) (number)))

====================================
Files without trailing terminators
Expand Down
51 changes: 35 additions & 16 deletions corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ done
(program
(for_statement
(variable_name)
(word)
(word)
(command_substitution (command (command_name (word)) (word) (word)))
(number)
(number)
(command_substitution (command (command_name (word)) (number) (number)))
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
(for_statement
Expand Down Expand Up @@ -123,14 +123,14 @@ done

(program
(c_style_for_statement
(word)
(binary_expression (word) (word))
(variable_assignment (variable_name) (number))
(binary_expression (word) (number))
(word)
(do_group
(command (command_name (word)) (simple_expansion (variable_name)))))
(c_style_for_statement
(word)
(binary_expression (word) (word))
(variable_assignment (variable_name) (number))
(binary_expression (word) (number))
(word)
(compound_statement
(command (command_name (word)) (simple_expansion (variable_name)))))
Expand Down Expand Up @@ -218,7 +218,7 @@ esac
(case_item (word) (word)
(command (command_name (word)))))
(case_statement (simple_expansion (variable_name))
(case_item (concatenation (word) (word))
(case_item (concatenation (word) (number))
(command (command_name (word)) (simple_expansion (special_variable_name))))))

=============================
Expand All @@ -242,7 +242,7 @@ fi
(command_name (word))
(string (simple_expansion (variable_name))))))
(command_substitution (command (command_name (word)) (raw_string)))))
(command (command_name (word)) (word))))
(command (command_name (word)) (number))))

=============================
Test commands with regexes
Expand Down Expand Up @@ -350,7 +350,7 @@ function do_yet_another_thing {
(redirected_statement (function_definition
(word)
(compound_statement (command (command_name (word)) (word))))
(file_redirect (file_descriptor) (word))))
(file_redirect (file_descriptor) (number))))

=========================================
Variable declaration: declare & typeset
Expand All @@ -364,8 +364,8 @@ typeset -i -r var2=42 var3=10
(program
(declaration_command (variable_name))
(declaration_command (word) (word)
(variable_assignment (variable_name) (word))
(variable_assignment (variable_name) (word))))
(variable_assignment (variable_name) (number))
(variable_assignment (variable_name) (number))))

=========================================
Variable declaration: readonly
Expand All @@ -378,7 +378,7 @@ readonly var2=42

(program
(declaration_command (variable_name))
(declaration_command (variable_assignment (variable_name) (word))))
(declaration_command (variable_assignment (variable_name) (number))))

=========================================
Variable declaration: local
Expand All @@ -391,7 +391,7 @@ local -r c

(program
(declaration_command
(variable_assignment (variable_name) (word))
(variable_assignment (variable_name) (number))
(variable_name))
(declaration_command
(word)
Expand Down Expand Up @@ -486,8 +486,27 @@ a () {
(command (command_name (word)))
(compound_statement
(command (command_name (word)) (string))
(command (command_name (word)) (word))))
(command (command_name (word)) (number))))
(command (command_name (word)) (word))))
(redirected_statement
(compound_statement (command (command_name (word)) (string)) (command (command_name (word)) (string)))
(file_redirect (word))))
(file_redirect (number))))

==========================
Arithmetic expansion
==========================

a=$((64#@ << 22))
b="$((-42 + ~2))"

---

(program
(variable_assignment
(variable_name)
(arithmetic_expansion (binary_expression (number) (number))))
(variable_assignment
(variable_name)
(string (arithmetic_expansion (binary_expression
(unary_expression (number))
(unary_expression (number)))))))
29 changes: 21 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = grammar({
c_style_for_statement: $ => seq(
'for',
'((',
optional($._expression),
optional(choice($.variable_assignment, $._expression)),
$._terminator,
optional($._expression),
$._terminator,
Expand Down Expand Up @@ -356,7 +356,7 @@ module.exports = grammar({
'=', '==', '=~', '!=',
'+', '-', '+=', '-=',
'<', '>', '<=', '>=',
'||', '&&',
'||', '&&', '<<', '>>',
$.test_operator
),
$._expression
Expand All @@ -368,10 +368,16 @@ module.exports = grammar({
)
)),

unary_expression: $ => prec.right(seq(
choice('!', $.test_operator),
$._expression
)),
unary_expression: $ => choice(
prec(1, seq(
token(prec(1, choice('-', '+', '~', '++', '--'))),
$._expression
)),
prec.right(seq(
choice('!', $.test_operator),
$._expression
)),
),

postfix_expression: $ => seq(
$._expression,
Expand All @@ -397,13 +403,17 @@ module.exports = grammar({
$.string,
$.raw_string,
$.ansii_c_string,
$.number,
$.expansion,
$.simple_expansion,
$.string_expansion,
$.command_substitution,
$.process_substitution
$.process_substitution,
$.arithmetic_expansion
),

arithmetic_expansion: $ => seq('$((', $._expression, '))'),

concatenation: $ => prec(-1, seq(
choice(
$._primary_expression,
Expand All @@ -428,7 +438,8 @@ module.exports = grammar({
seq(optional('$'), $._string_content),
$.expansion,
$.simple_expansion,
$.command_substitution
$.command_substitution,
$.arithmetic_expansion,
),
optional($._concat)
)),
Expand All @@ -448,6 +459,8 @@ module.exports = grammar({

ansii_c_string: $ => /\$'([^']|\\')*'/,

number: $ => /(0x)?[0-9]+(#[0-9A-Za-z@_]+)?/,

simple_expansion: $ => seq(
'$',
choice(
Expand Down
Loading