Skip to content
Browse files

readme

  • Loading branch information...
1 parent 6d21b9a commit a021b95bc51bb18b110933d942449c26ce708bf8 @itod committed Jan 26, 2010
Showing with 38 additions and 20 deletions.
  1. +38 −20 README.textile
View
58 README.textile
@@ -24,6 +24,7 @@ h2. Projects using ParseKit:
"Exedore":http://tr.im/exedore: XPath 1.0 implemented in Cocoa (ported from "Saxon":http://saxonica.com/)
h2. Xcode Project
+
The ParseKit Xcode project consists of 6 targets:
**ParseKit** : the ParseKit Objective-C framework. The central feature/codebase of this project.
@@ -41,6 +42,7 @@ The API for tokenization is provided by the PKTokenizer class. Cocoa developers
Example usage:
+<pre>
NSString *s = @""It's 123 blast-off!", she said, // watch out!n"
@"and <= 3.5 'ticks' later /* wince */, it's blast-off!";
PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
@@ -53,6 +55,7 @@ while ((tok = [t nextToken]) != eof) {
outputs:
+<pre>
("It's 123 blast-off!")
(,)
(she)
@@ -73,11 +76,13 @@ Each token produced is an object of class PKToken. PKTokens have a tokenType (Wo
More information about a token can be easily discovered using the -debugDescription method instead of the default -description. Replace the line containing NSLog above with this line:
+<pre>
NSLog(@" (%@)", [tok debugDescription]);
</pre>
and each token's type will be printed as well:
+<pre>
<Quoted String «"It's 123 blast-off!"»>
<Symbol «,»>
<Word «she»>
@@ -96,63 +101,76 @@ and each token's type will be printed as well:
As you can see from the output, PKTokenzier is configured by default to properly group characters into tokens including:
-single- and double-quoted string tokens
-common multiple character symbols (<=)
-apostrophes, dashes and other symbol chars that should not signal the start of a new Symbol token, but rather be included in the current Word or Num token (it's, blast-off, 3.5)
-silently ignoring C- and C++-style comments
-silently ignoring whitespace
+* single- and double-quoted string tokens
+* common multiple character symbols (<=)
+* apostrophes, dashes and other symbol chars that should not signal the start of a new Symbol token, but rather be included in the current Word or Num token (it's, blast-off, 3.5)
+* silently ignoring C- and C++-style comments
+* silently ignoring whitespace
The PKTokenizer class is very flexible, and **all** of those features are configurable. PKTokenizer may be configured to:
-recognize more (or fewer) multi-char symbols. ex: p. [t.symbolState add:@"!="];</pre>
+* recognize more (or fewer) multi-char symbols. ex:
+
+<pre>[t.symbolState add:@"!="];</pre>
+
allows != to be recognized as a single Symbol token rather than two adjacent Symbol tokens
-add new internal symbol chars to be included in the current Word token OR recognize internal symbols like apostrophe and dash to actually signal a new Symbol token rather than being part of the current Word token. ex:
-p. [t.wordState setWordChars:YES from:'_' to:'_'];</pre>
+*add new internal symbol chars to be included in the current Word token OR recognize internal symbols like apostrophe and dash to actually signal a new Symbol token rather than being part of the current Word token. ex:
+
+<pre>[t.wordState setWordChars:YES from:'_' to:'_'];</pre>
+
allows Word tokens to contain internal underscores
-p. [t.wordState setWordChars:NO from:'-' to:'-'];</pre>
+
+<pre>[t.wordState setWordChars:NO from:'-' to:'-'];</pre>
+
disallows Word tokens from containing internal dashes.
-change which chars singnal the start of a token of any given type. ex:
-p. [t setTokenizerState:t.wordState from:'_' to:'_'];</pre>
+* change which chars singnal the start of a token of any given type. ex:
+
+<pre>[t setTokenizerState:t.wordState from:'_' to:'_'];</pre>
+
allows Word tokens to start with underscore
-p. [t setTokenizerState:t.quoteState from:'*' to:'*'];</pre>
+
+<pre>[t setTokenizerState:t.quoteState from:'*' to:'*'];</pre>
allows Quoted String tokens to start with an asterisk, effectively making * a new quote symbol (like " or ')
-turn off recognition of single-line "slash-slash" (//) comments. ex:
-p. [t setTokenizerState:t.symbolState from:'/' to:'/'];</pre>
+* turn off recognition of single-line "slash-slash" (//) comments. ex:
+
+<pre>[t setTokenizerState:t.symbolState from:'/' to:'/'];</pre>
+
slash chars now produce individual Symbol tokens rather than causing the tokenizer to strip text until the next newline char or begin striping for a multiline comment if appropriate (/*)
-turn on recognition of "hash" (#) single-line comments. ex:
+* turn on recognition of "hash" (#) single-line comments. ex:
-[t setTokenizerState:t.commentState from:'#' to:'#'];
+<pre>[t setTokenizerState:t.commentState from:'#' to:'#'];
[t.commentState addSingleLineStartSymbol:@"#"];</pre>
-turn on recognition of "XML/HTML" (<!-- -->) multi-line comments. ex:
+* turn on recognition of "XML/HTML" (<!-- -->) multi-line comments. ex:
<pre>[t setTokenizerState:t.commentState from:'<' to:'<'];
[t.commentState addMultiLineStartSymbol:@"<!--" endSymbol:@"-->"];</pre>
-report (rather than silently consume) Comment tokens. ex:
+* report (rather than silently consume) Comment tokens. ex:
<pre>t.commentState.reportsCommentTokens = YES; // default is NO</pre>
-report (rather than silently consume) Whitespace tokens. ex:
+* report (rather than silently consume) Whitespace tokens. ex:
<pre>t.whitespaceState.reportsWhitespaceTokens = YES; // default is NO</pre>
-turn on recognition of any characters (say, digits) as whitespace to be silently ignored. ex:
+* turn on recognition of any characters (say, digits) as whitespace to be silently ignored. ex:
<pre>[t setTokenizerState:t.whitespaceState from:'0' to:'9'];</pre>
h3. Parsing
+
ParseKit also includes a collection of token parser subclasses (of the abstract PKParser class) including collection parsers such as PKAlternation, PKSequence, and PKRepetition as well as terminal parsers including PKWord, PKNum, PKSymbol, PKQuotedString, etc. Also included are parser subclasses which work in individual chars such as PKChar, PKDigit, and PKSpecificChar. These char parsers are useful for things like RegEx parsing. Generally speaking though, the token parsers will be more useful and interesting.
The parser classes represent a **Composite** pattern. Programs can build a composite parser, in **Objective-C** (rather than a separate language like with lex&yacc), from a collection of terminal parsers composed into alternations, sequences, and repetitions to represent an infinite number of languages.
Parsers built from ParseKit are **non-deterministic, recursive descent parsers**, which basically means they trade some performance for ease of user programming and simplicity of implementation.

0 comments on commit a021b95

Please sign in to comment.
Something went wrong with that request. Please try again.