1lexer grammar t012lexerXMLLexer; 2options { 3 language =Cpp; 4} 5 6@lexer::includes 7{ 8#include "UserTestTraits.hpp" 9#include <iostream> 10} 11@lexer::namespace 12{ Antlr3Test } 13 14@lexer::context { 15ImplTraits::StringStreamType outbuf; 16 17void output(const char* line) 18{ 19 outbuf << line << "\r\n"; 20} 21 22void output(const char* line1, const char *line2) 23{ 24 outbuf << line1 << line2 << "\r\n"; 25} 26 27void output(const char* line1, ImplTraits::StringType const& line2) 28{ 29 outbuf << line1 << line2 << "\r\n"; 30} 31 32void appendArribute(const char* prefix, ImplTraits::StringType const& name, ImplTraits::StringType const& value) 33{ 34 outbuf << prefix << name << '=' << value << "\r\n"; 35} 36 37void appendString(const char* name, ImplTraits::StringType const& value) 38{ 39 outbuf << name << '"' << value << '"' << "\r\n"; 40} 41 42} 43DOCUMENT 44 : XMLDECL? WS? DOCTYPE? WS? ELEMENT WS? 45 ; 46 47fragment DOCTYPE 48 : 49 '<!DOCTYPE' WS rootElementName=GENERIC_ID 50 { output("ROOTELEMENT: ", $rootElementName.text);} 51 WS 52 ( 53 ( 'SYSTEM' WS sys1=VALUE 54 {output("SYSTEM: ", $sys1.text);} 55 56 | 'PUBLIC' WS pub=VALUE WS sys2=VALUE 57 {output("PUBLIC: ", $pub.text);} 58 {output("SYSTEM: ", $sys2.text);} 59 ) 60 ( WS )? 61 )? 62 ( dtd=INTERNAL_DTD 63 {output("INTERNAL DTD: ", $dtd.text);} 64 )? 65 '>' 66 ; 67 68fragment INTERNAL_DTD : '[' (options {greedy=false;} : .)* ']' ; 69 70fragment PI : 71 '<?' target=GENERIC_ID WS? 72 {output("PI: ", $target.text);} 73 ( ATTRIBUTE WS? )* '?>' 74 ; 75 76fragment XMLDECL : 77 '<?' ('x'|'X') ('m'|'M') ('l'|'L') WS? 78 {output("XML declaration");} 79 ( ATTRIBUTE WS? )* '?>' 80 ; 81 82 83fragment ELEMENT 84 : ( START_TAG 85 (ELEMENT 86 | t=PCDATA 87 {appendString("PCDATA: ", $t.text);} 88 | t=CDATA 89 {appendString("CDATA: ", $t.text);} 90 | t=COMMENT 91 {appendString("Comment: ", $t.text);} 92 | pi=PI 93 )* 94 END_TAG 95 | EMPTY_ELEMENT 96 ) 97 ; 98 99fragment START_TAG 100 : '<' WS? name=GENERIC_ID WS? 101 {output("Start Tag: ", $name.text);} 102 ( ATTRIBUTE WS? )* '>' 103 ; 104 105fragment EMPTY_ELEMENT 106 : '<' WS? name=GENERIC_ID WS? 107 {output("Empty Element: ", $name.text);} 108 ( ATTRIBUTE WS? )* '/>' 109 ; 110 111fragment ATTRIBUTE 112 : name=GENERIC_ID WS? '=' WS? value=VALUE 113 {appendArribute("Attr: ", $name.text, $value.text);} 114 ; 115 116fragment END_TAG 117 : '</' WS? name=GENERIC_ID WS? '>' 118 {output("End Tag: ", $name.text);} 119 ; 120 121fragment COMMENT 122 : '<!--' (options {greedy=false;} : .)* '-->' 123 ; 124 125fragment CDATA 126 : '<![CDATA[' (options {greedy=false;} : .)* ']]>' 127 ; 128 129fragment PCDATA : (~'<')+ ; 130 131fragment VALUE : 132 ( '\"' (~'\"')* '\"' 133 | '\'' (~'\'')* '\'' 134 ) 135 ; 136 137fragment GENERIC_ID 138 : ( LETTER | '_' | ':') 139 ( options {greedy=true;} : LETTER | '0'..'9' | '.' | '-' | '_' | ':' )* 140 ; 141 142fragment LETTER 143 : 'a'..'z' 144 | 'A'..'Z' 145 ; 146 147fragment WS : 148 ( ' ' 149 | '\t' 150 | ( '\n' 151 | '\r\n' 152 | '\r' 153 ) 154 )+ 155 ; 156 157