I'm trying to implement a grammar for parsing queries. Single query consists of items
where each item can be either name
or name-ref
.
name
is either mystring
(only letters, no spaces) or "my long string"
(letters and spaces, always quoted). name-ref
is very similar to name
and the only difference is that it should start with ref:
(ref:mystring
, ref:"my long string"
). Query should contain at least 1 item (name
or name-ref
).
Here's what I have:
NAME: ('a'..'z')+;
REF_TAG: 'ref:';
SP: ' '+;
name: NAME;
name_ref: REF_TAG name;
item: name | name_ref;
query: item (SP item)*;
This grammar demonstrates what I basically need to get and the only feature is that it doesn't support long quoted strings (it works fine for names that doesn't have spaces).
SHORT_NAME: ('a'..'z')+;
LONG_NAME: SHORT_NAME (SP SHORT_NAME)*;
REF_TAG: 'ref:';
SP: ' '+;
Q: '"';
short_name: SHORT_NAME;
long_name: LONG_NAME;
name_ref: REF_TAG (short_name | (Q long_name Q));
item: (short_name | (Q long_name Q)) | name_ref;
query: item (SP item)*;
But that doesn't work. Any ideas what's the problem? Probably, that's important: my first query
should be treated as 3 item
s (3 name
s) and "my first query"
is 1 item
(1 long_name
).