question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to avoid ambiguity – parsing quoted, unquoted and strings containing quotes

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kachcommented, Jul 9, 2018

Do you mean

subroutine: foo
  - bar["    <---- bar, not foo
  - "]baz

in the test case?

1reaction
kachcommented, Nov 28, 2017

I think the right answer here is for you to use a lexer. @tjvr ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found