How to avoid ambiguity – parsing quoted, unquoted and strings containing quotes
See original GitHub issueI want to able to parse all three variations:
- a string quoted using single quotes, e.g.
'foo'
- a string quoted using double quotes, e.g.
"foo"
- a string that contains single/ double quotes, e.g.
foo'
,fo'o
, etc.
I have written the following grammar:
@{%
const appendItem = (a, b) => d => d[a].concat([d[b]]);
%}
subroutines ->
subroutines _ "|" _ subroutine {% appendItem(0, 4) %}
| subroutine
subroutine ->
subroutineName " " parameters {% d => ({subroutine: d[0], values: d[2]}) %}
| subroutineName {% d => ({subroutine: d[0], values: []}) %}
subroutineName ->
[a-zA-Z0-9\-_]:+ {% d => d[0].join('') %}
parameters ->
parameters " " parameter {% appendItem(0, 2) %}
| parameter
parameter ->
sqstring {% id %}
| dqstring {% id %}
| nqstring {% id %}
dqstring ->
"\"" dstrchar:* "\"" {% d => d[1].join('') %}
dstrchar ->
[^"] {% id %}
| "\\\"" {% d => '"' %}
sqstring ->
"'" sstrchar:* "'" {% d => d[1].join('') %}
sstrchar ->
[^'] {% id %}
| "\\'" {% d => '\'' %}
nqstring ->
[^| ]:+
_ ->
[ ]:* {% d => null %}
And testing it using nearley-playground with the following inputs:
foo "a b
foo a b"
foo "a"
However, I cannot figure out how to avoid ambiguity between nqstring
and dqstring
/ sqstring
?
Related issue: https://github.com/gajus/pianola/issues/1
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Problems parsing quoted/unquoted strings. - Google Groups
The text can be double quoted, single quoted or unquoted. With quotes, there's no problem, without, I can't seem to be able to...
Read more >YAML: Do I need quotes for strings in YAML? - Stack Overflow
Quoting all strings as some suggest, is like using brackets in python. It is bad practice, harms readability and throws away the beautiful ......
Read more >19 Quasiquotation | Advanced R - Hadley Wickham
Section 19.3 gives you the tools to quote expressions, whether they come from you or the user, or whether you use rlang or...
Read more >Improved Identifiers - JMESPath
It's ambiguous which rule to use. Given a string “123”, it's not clear whether this should be parsed as an identifier or a...
Read more >9.2 Schema Object Names
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Do you mean
in the test case?
I think the right answer here is for you to use a lexer. @tjvr ?