Skip to content

Instantly share code, notes, and snippets.

@rns
Created September 5, 2014 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rns/96647c9205db54e087a5 to your computer and use it in GitHub Desktop.
Save rns/96647c9205db54e087a5 to your computer and use it in GitHub Desktop.
use 5.010;
use strict;
use warnings;
use Marpa::R2;
use YAML;
my $g = Marpa::R2::Scanless::G->new( {
source => \(<<'END_OF_SOURCE'),
:default ::= action => [name, value]
lexeme default = action => [name, value] latm => 1
S ::= quote text quote
text ::= in_quotes+
in_quotes ::= non_quote | escaped_quote
quote ~ '"';
non_quote ~ [^"\\]+ # "
escaped_quote ~ '\"'
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my $input = <<EOI;
"before escaped quotes \\"in escaped quotes\\" after escaped quotes \\"again in escaped quotes\\" and some text again"
EOI
my $r = Marpa::R2::Scanless::R->new( { grammar => $g, trace_terminals => 1 } );
eval {$r->read(\$input)} || die "\nParse failure, progress report is:\n" . $r->show_progress;
say Dump ${ $r->value };
@rns
Copy link
Author

rns commented Sep 5, 2014

- S
-
  - quote
  - '"'
-
  - text
  -
    - in_quotes
    -
      - non_quote
      - 'before escaped quotes '
  -
    - in_quotes
    -
      - escaped_quote
      - \"
  -
    - in_quotes
    -
      - non_quote
      - in escaped quotes
  -
    - in_quotes
    -
      - escaped_quote
      - \"
  -
    - in_quotes
    -
      - non_quote
      - ' after escaped quotes '
  -
    - in_quotes
    -
      - escaped_quote
      - \"
  -
    - in_quotes
    -
      - non_quote
      - again in escaped quotes
  -
    - in_quotes
    -
      - escaped_quote
      - \"
  -
    - in_quotes
    -
      - non_quote
      - ' and some text again'
-
  - quote
  - '"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment