[1] | 1 | |
---|
| 2 | #include <iostream> |
---|
| 3 | |
---|
| 4 | #include "DemoJavaLexer.hpp" |
---|
| 5 | #include "DemoJavaDocLexer.hpp" |
---|
| 6 | #include "DemoJavaParser.hpp" |
---|
| 7 | #include "antlr/TokenStreamSelector.hpp" |
---|
| 8 | |
---|
| 9 | int main() |
---|
| 10 | { |
---|
| 11 | ANTLR_USING_NAMESPACE(std) |
---|
| 12 | ANTLR_USING_NAMESPACE(antlr) |
---|
| 13 | |
---|
| 14 | // Define a selector that can switch from java to javadoc |
---|
| 15 | TokenStreamSelector selector; |
---|
| 16 | |
---|
| 17 | try { |
---|
| 18 | // attach java lexer to the input stream, which also creates a shared input state object |
---|
| 19 | DemoJavaLexer main(cin); |
---|
| 20 | main.setSelector(&selector); |
---|
| 21 | |
---|
| 22 | // create javadoc lexer; attach to same shared input state as java lexer |
---|
| 23 | DemoJavaDocLexer doclexer(main.getInputState()); |
---|
| 24 | doclexer.setSelector(&selector); |
---|
| 25 | |
---|
| 26 | // notify selector about various lexers; name them for convenient reference later |
---|
| 27 | selector.addInputStream(&main, "main"); |
---|
| 28 | selector.addInputStream(&doclexer, "doclexer"); |
---|
| 29 | selector.select("main"); // start with main java lexer |
---|
| 30 | |
---|
| 31 | // Create parser attached to selector |
---|
| 32 | DemoJavaParser parser(selector); |
---|
| 33 | |
---|
| 34 | // Pull in one or more int decls with optional javadoc |
---|
| 35 | parser.input(); |
---|
| 36 | |
---|
| 37 | // spin thru all tokens generated via the SELECTOR. |
---|
| 38 | RefToken t; |
---|
| 39 | while ( (t=selector.nextToken())->getType()!=Token::EOF_TYPE ) { |
---|
| 40 | cout << t->toString() << endl; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | catch( ANTLRException& e ) |
---|
| 44 | { |
---|
| 45 | cerr << "exception: " << e.getMessage() << endl; |
---|
| 46 | return -1; |
---|
| 47 | } |
---|
| 48 | catch( exception& e ) |
---|
| 49 | { |
---|
| 50 | cerr << "exception: " << e.what() << endl; |
---|
| 51 | return -1; |
---|
| 52 | } |
---|
| 53 | return 0; |
---|
| 54 | } |
---|
| 55 | |
---|