Cannot Define a Range of Characters using ParseKit's Grammar.
(Letters Ranging from A to Z with or without Capitalization).
I am Generating a Parser using ParseKit's ParserGenApp.
The Parser is for a .Framework and functions as a Custom Language Parser.
The Parser is to mimic a working Parser that was Generated using Antlr4 for Java.
Antlr4: CHAR : [a-zA-Z];
Regular Expression: "[a-zA-Z]"
which would detect any Letter from 'a' to 'z' including Capitalizations
Alternate Regular Expression: "[c-xB-Y]"
How can I Grammatically define a Range of Characters using the ParserGenApp?
Is there a Grammatical Directive in ParseKit like 'Word' or 'Number' for Characters/Letters?
Is there an Alternative way to declare a Range?
Creator of ParseKit here.
ParseKit is currently undergoing a bit of a redesign. There are two ways to use ParseKit: the old way and the new way.
OLD WAY (dynamic): Previously, ParseKit produced dynamic, non-deterministic parsers at runtime (this code is still available in the library). Producing these parsers was slow, and the parsers produced were very slow as well (although they had some interesting properties which are useful in very rare circumstances).
NEW WAY (static): Now, using the ParserGenApp (as you've described here), ParseKit produces static ObjC source code for deterministic (PEG) memoizing parsers. Source code is produced at design time which you can then compile into your project. The parsers produced are fast. This new option is now the preferred method of using ParseKit. The old method will be deprecated somehow.
I will assume you are using the new static method (it sounds like you are already from your question).
Here are two ways to match "words" in your ParseKit grammars:
Word
rule reference which is equivalent to [_a-zA-Z][-'_a-zA-Z0-9]*
, and will usually do what you want:username = Word;
Word
terminal does not match exactly what you want, prefix it with a Syntactic Predicate (idea/syntax stolen from ANTLR3) containing a Regex in the MATCHES()
macro:username = { MATCHES(@"[c-xB-Y]", LS(1)) }? Word;
OR:
username = { MATCHES_IGNORE_CASE(@"[a-z]", LS(1)) }? Word;
{ ... }?
placed just before a rule reference (Word
in this case).MATCHES()
, MATCHES_IGNORE_CASE()
, and LS()
are just C macros I have made available for convenience.MATCHES()
and MATCHES_IGNORE_CASE()
macros are a shortcut for using NSRegularExpression
.LS()
macro stands for L ookahead S tring. LS(1)
means fetch the NSString
value of the first lookahead token. In this case the first lookahead token will be the token matched by Word
. To look ahead two or three tokens, you would use LS(2)
, LS(3)
and so forth.