alternatives completion list

42 views
Skip to first unread message

krupa....@gmail.com

unread,
Dec 31, 2012, 4:43:06 AM12/31/12
to pe...@googlegroups.com
Assume I've got grammar like:

start = ('a' / 'b')+

and the following input in textarea:

"a_" (where `_` is keyboard input caret position).

Is it possible to get completions list in `_`position? Completions list I would expect in that place is ['a', 'b'].

David Majda

unread,
Jan 2, 2013, 2:56:21 PM1/2/13
to krupab...@gmail.com, pegjs
2012/12/31 <krupa....@gmail.com>:
This is not supported directly, but there is a slightly hackish way to
implement this: take the input until the caret position and let
PEG.js-generated parser parse this string. It will throw an exception
(complaining about reaching an end of input) whose "expected" property
will contain the completion list.

There is one caveat -- this won't work if the substring passed to the
parser is actually a valid input according to your grammar. In that
case you can just append some string that is guaranteed to be invalid
and do the same trick. But this may not work 100% for all grammars in
all situations.

--
David Majda
Entropy fighter
http://majda.cz/
Reply all
Reply to author
Forward
0 new messages