From ac3f069838338778c9f3bbdc879ad44662d64a05 Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Tue, 3 Dec 2024 16:22:03 -0600 Subject: [PATCH] Make array and hash access in interpolation allow parentheses. This is still not as versatile as it needs to be. Operations should also be allowed in array indices and hash keys. --- package-lock.json | 4 +- package.json | 2 +- src/pg.grammar | 4 +- test/perl-quote-and-quote-like-operators.txt | 46 ++++++++++++++++---- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index d15f3d2..c7ea0b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@openwebwork/codemirror-lang-pg", - "version": "0.0.1-beta.18", + "version": "0.0.1-beta.19", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@openwebwork/codemirror-lang-pg", - "version": "0.0.1-beta.18", + "version": "0.0.1-beta.19", "license": "MIT", "dependencies": { "@codemirror/language": "^6.10.2", diff --git a/package.json b/package.json index 5e24e52..1ce59e3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/pg.grammar b/src/pg.grammar index 0369f9a..5426464 100644 --- a/src/pg.grammar +++ b/src/pg.grammar @@ -431,7 +431,7 @@ semicolon { StatementEnd | automaticSemicolon } !arrayMember (ArrowOperator !arrowOperator)? "[" - (Integer | ArithOp<"-"> Integer | Float | ScalarVariable | PGVariable | quotedString) + maybeParens? Integer | Float | ScalarVariable | PGVariable | quotedString> "]" } HashAccessVariable { @@ -440,7 +440,7 @@ semicolon { StatementEnd | automaticSemicolon } !hashMember (ArrowOperator !arrowOperator)? "{" - ((!bareword Identifier) | ScalarVariable | PGVariable | quotedString) + (!bareword Identifier | maybeParens) "}" } diff --git a/test/perl-quote-and-quote-like-operators.txt b/test/perl-quote-and-quote-like-operators.txt index 5a57b5d..e0813b1 100644 --- a/test/perl-quote-and-quote-like-operators.txt +++ b/test/perl-quote-and-quote-like-operators.txt @@ -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']}"; @@ -15,7 +15,16 @@ 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( @@ -23,7 +32,7 @@ Program( InterpolatedStringContent, ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", Integer, "]"), InterpolatedStringContent, - ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", ArithOp, Integer, "]"), + ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", "(", ArithOp, Integer, ")", "]"), InterpolatedStringContent, ArrayAccessVariable( ArrayAccessVariable(ScalarVariable, ArrowOperator, "[", Integer, "]"), @@ -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( @@ -64,7 +86,13 @@ Program( "{", Identifier, "}" - ) + ), + InterpolatedStringContent, + HashAccessVariable(ScalarVariable, ArrowOperator, "{", ScalarVariable, "}"), + InterpolatedStringContent, + HashAccessVariable(ScalarVariable, ArrowOperator, "{", "(", StringSingleQuoted, ")", "}"), + InterpolatedStringContent, + HashAccessVariable(ScalarVariable, ArrowOperator, "{", "(", ScalarVariable, ")", "}") ), StatementEnd(";") ),