Skip to content

Commit

Permalink
Make array and hash access in interpolation allow parentheses.
Browse files Browse the repository at this point in the history
This is still not as versatile as it needs to be.  Operations should
also be allowed in array indices and hash keys.
  • Loading branch information
drgrice1 committed Dec 3, 2024
1 parent ba972ad commit ac3f069
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openwebwork/codemirror-lang-pg",
"version": "0.0.1-beta.18",
"version": "0.0.1-beta.19",
"description": "PG language support for CodeMirror",
"author": "The WeBWorK Project",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/pg.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ semicolon { StatementEnd | automaticSemicolon }
!arrayMember
(ArrowOperator !arrowOperator)?
"["
(Integer | ArithOp<"-"> Integer | Float | ScalarVariable | PGVariable | quotedString)
maybeParens<ArithOp<"-">? Integer | Float | ScalarVariable | PGVariable | quotedString>
"]"
}
HashAccessVariable {
Expand All @@ -440,7 +440,7 @@ semicolon { StatementEnd | automaticSemicolon }
!hashMember
(ArrowOperator !arrowOperator)?
"{"
((!bareword Identifier) | ScalarVariable | PGVariable | quotedString)
(!bareword Identifier | maybeParens<ScalarVariable | PGVariable | quotedString>)
"}"
}

Expand Down
46 changes: 37 additions & 9 deletions test/perl-quote-and-quote-like-operators.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# double quoted strings

"variable: $var";
"array access: $a[1]";
"deref array access: $a->[1] $a->[-1] $b->[1][1] $c->[1]->[1]";
"hash access: $var{b}";
"deref hash access: $var->{b} $var2->{a}{b} $var3->{a}->{b}";
"array access: $a[1] $a[(-1)] $a[($index)] $a[$index]";
"deref array access: $a->[1] $a->[(-1)] $b->[1][1] $c->[1]->[1] $d->[$index] $d->[($index)]";
"hash access: $var{b} $var{$key} $var{('b')} $var{($key)}";
"deref hash access: $var->{b} $var2->{a}{b} $var3->{a}->{b} $var4->{$key} $var4->{('a')} $var4->{($key)}";
"mixed deref hash and array access: $var1->{a}[1] $var2->{a}->[1] $var3->[1]{a} $var4->[1]->{a}";
"dereferenced scalar reference ${~~($a + 2)}";
"dereferenced arary reference @{['a' . 'b']}";
Expand All @@ -15,15 +15,24 @@
Program(
ExpressionStatement(StringDoubleQuoted(InterpolatedStringContent, ScalarVariable), StatementEnd(";")),
ExpressionStatement(
StringDoubleQuoted(InterpolatedStringContent, ArrayAccessVariable(ScalarVariable, "[", Integer, "]")),
StringDoubleQuoted(
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, "[", Integer, "]"),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, "[", "(", ArithOp, Integer, ")", "]"),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, "[", "(", ScalarVariable, ")", "]"),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, "[", ScalarVariable, "]")
),
StatementEnd(";")
),
ExpressionStatement(
StringDoubleQuoted(
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", Integer, "]"),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", ArithOp, Integer, "]"),
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", "(", ArithOp, Integer, ")", "]"),
InterpolatedStringContent,
ArrayAccessVariable(
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", Integer, "]"),
Expand All @@ -38,12 +47,25 @@ Program(
"[",
Integer,
"]"
)
),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", ScalarVariable, "]"),
InterpolatedStringContent,
ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", "(", ScalarVariable, ")", "]")
),
StatementEnd(";")
),
ExpressionStatement(
StringDoubleQuoted(InterpolatedStringContent, HashAccessVariable(ScalarVariable, "{", Identifier, "}")),
StringDoubleQuoted(
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, "{", Identifier, "}"),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, "{", ScalarVariable, "}"),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, "{", "(", StringSingleQuoted, ")", "}"),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, "{", "(", ScalarVariable, ")", "}")
),
StatementEnd(";")
),
ExpressionStatement(
Expand All @@ -64,7 +86,13 @@ Program(
"{",
Identifier,
"}"
)
),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, ArrowOperator, "{", ScalarVariable, "}"),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, ArrowOperator, "{", "(", StringSingleQuoted, ")", "}"),
InterpolatedStringContent,
HashAccessVariable(ScalarVariable, ArrowOperator, "{", "(", ScalarVariable, ")", "}")
),
StatementEnd(";")
),
Expand Down

0 comments on commit ac3f069

Please sign in to comment.