Mercurial > hg > xslscript
comparison xsls.pl @ 2:bcd96c403898
XSLScript: rewritten grammar, matches all xsls.
While this grammar may be incomplete and/or incorrect in some cases,
it is able to match all xsls files in nginx.org.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 20 Feb 2014 07:38:58 +0400 |
parents | f082f8c2ebb0 |
children | 0fa786e03744 |
comparison
equal
deleted
inserted
replaced
1:f082f8c2ebb0 | 2:bcd96c403898 |
---|---|
34 my $grammar = <<'EOF'; | 34 my $grammar = <<'EOF'; |
35 | 35 |
36 # XSLTScript grammar, reconstructed | 36 # XSLTScript grammar, reconstructed |
37 | 37 |
38 startrule : item(s) eofile | 38 startrule : item(s) eofile |
39 { $return = $item[1] } | 39 { $return = $item[1] } |
40 | 40 |
41 #item : "<!--" <commit> comment | 41 item : "<!--" <commit> comment |
42 # | "!" <commit> shortcut | 42 | "!!" <commit> double_exclam |
43 # | "<%" <commit> instruction "%>" | 43 | "!{" <commit> exclam_xpath |
44 # | instruction_name <commit> | 44 | "!" name <commit> exclam_name |
45 | 45 | "<%" <commit> instruction "%>" |
46 item : comment | instruction | shortcut | 46 | "<" name attrs ">" <commit> item(s?) "</" name ">" |
47 item_text : comment | instruction | shortcut | text | 47 | "<" <commit> name attrs "/" ">" |
48 | "X:variable" <commit> xvariable | |
49 | "X:var" <commit> xvariable | |
50 | "X:template" <commit> xtemplate | |
51 | "X:if" <commit> xif | |
52 | "X:param" <commit> xparam | |
53 | "X:for-each" <commit> xforeach | |
54 | "X:sort" <commit> xsort | |
55 | "X:when" <commit> xwhen | |
56 | "X:attribute" <commit> xattribute | |
57 | "X:output" <commit> xoutput | |
58 | "X:copy-of" <commit> xcopyof | |
59 | instruction <commit> attrs body | |
60 | text | |
61 | <error> | |
62 | |
63 # list of simple instructions | |
64 | |
65 instruction : "X:stylesheet" | |
66 | "X:transform" | |
67 | "X:attribute-set" | |
68 | "X:element" | |
69 | "X:apply-templates" | |
70 | "X:choose" | |
71 | "X:otherwise" | |
72 | "X:value-of" | |
73 | "X:apply-imports" | |
74 | "X:number" | |
75 | "X:include" | |
76 | "X:import" | |
77 | "X:strip-space" | |
78 | "X:preserve-space" | |
79 | "X:copy" | |
80 | "X:text" | |
81 | "X:comment" | |
82 | "X:processing-instruction" | |
83 | "X:decimal-format" | |
84 | "X:namespace-alias" | |
85 | "X:key" | |
86 | "X:fallback" | |
87 | "X:message" | |
48 | 88 |
49 # comments, <!-- ... --> | 89 # comments, <!-- ... --> |
50 # not sure if it's something to be interpreted specially | 90 # not sure if it's something to be interpreted specially |
51 # likely an artifact of our dump process | 91 # likely an artifact of our dump process |
52 | 92 |
53 comment : "<!--" <commit> /((?!-->).)*/ms "-->" | 93 comment : /((?!-->).)*/ms "-->" |
54 { $return = ""; 1; } | 94 { $return = ""; 1; } |
55 | 95 |
56 # special chars: ', ", {, }, \ | 96 # special chars: ', ", {, }, \ |
57 # if used in text, they needs to be escaped with backslash | 97 # if used in text, they needs to be escaped with backslash |
58 | 98 |
59 text : quoted | unreserved | "'" | "\"" | 99 text : quoted | unreserved | "'" | "\"" | "{" |
60 quoted : "\\" special | 100 quoted : "\\" special |
61 { $return = $item{special}; 1; } | 101 { $return = $item{special}; 1; } |
62 special : "'" | "\"" | "\\" | "{" | "}" | 102 special : "'" | "\"" | "\\" | "{" | "}" |
63 unreserved : /[^'"\\{}]/ | 103 unreserved : /[^'"\\{}]/ |
64 | 104 |
66 # | 106 # |
67 # !{xpath-expression} for X:value-of select="xpath-expression"; | 107 # !{xpath-expression} for X:value-of select="xpath-expression"; |
68 # !! for X:apply-templates | 108 # !! for X:apply-templates |
69 # !foo() for X:call-template name="foo" | 109 # !foo() for X:call-template name="foo" |
70 | 110 |
71 shortcut : double_exclam | exclam_xpath | exclam_name | 111 # !root (path = { !{ substring($DIRNAME, 2) } }) |
72 double_exclam : "!!" <commit> param(s?) ";" | 112 # !root (path = "substring-after($path, '/')") |
73 exclam_xpath : "!{" <commit> text(s) "}" | 113 |
74 exclam_name : "!" <commit> name "(" param(s?) ")" | 114 double_exclam : value(?) params attrs ";" |
115 | |
116 exclam_xpath : xpath(s?) "}" | |
117 xpath : /[^}'"]+/ | |
118 | /"[^"]*"/ | |
119 | /'[^']*'/ | |
120 | |
121 exclam_name : params | |
122 | |
123 # instruction attributes | |
124 # name="value" | |
125 | |
126 attrs : attr(s?) | |
127 attr : name "=" value | |
75 name : /[a-z0-9_:-]+/i | 128 name : /[a-z0-9_:-]+/i |
76 | 129 value : /"[^"]*"/ |
77 param : param_name "=" param_value | 130 |
78 param_name : /[a-z0-9_:-]+/i | 131 # template parameters |
79 param_value : "\"" /[^"]*/ "\"" | 132 # ( bar="init", baz={markup} ) |
133 | |
134 params : "(" param ("," param)(s?) ")" | |
135 | "" | |
136 param : name "=" value | |
137 | name "=" <commit> "{" item(s) "}" | |
138 | name | |
139 | |
140 # instruction body | |
141 # ";" for empty body, "{ ... }" otherwise | |
80 | 142 |
81 body : ";" | 143 body : ";" |
82 | "{" <commit> item_text(s?) "}" | 144 | "{" <commit> item(s?) "}" |
83 | 145 |
84 instructions : xstylesheet | xtransform | 146 # special handling of some instructions |
85 | xattributeset | xattribute | xelement | 147 # X:if attribute is test= |
86 | xparam | xapplytemplates | 148 |
87 | xforeach | xchoose | xwhen | xotherwise | 149 xif : value(?) attrs body "else" <commit> body |
88 | xvalueof | xapplyimports | xnumber | 150 | value(?) attrs body |
89 | xoutput | xinclude | ximport | xstripspace | xpreservespace | 151 | <error> |
90 | xcopyof | xcopy | xtext | xsort | xcomment | |
91 | xprocessinginstruction | xdecimalformat | xnamespacealias | |
92 | xkey | xfallback | xmessage | |
93 | |
94 instruction : "<%" instruction_simple "%>" | |
95 | instruction_simple | |
96 | |
97 instruction_simple : instructions <commit> param(s?) body | |
98 | xif | |
99 | xtemplate | |
100 | xvar | |
101 | |
102 # X:if parameter is test= | |
103 | |
104 xif : "X:if" param_value(?) param(s?) body "else" <commit> body | |
105 | "X:if" <commit> param_value(?) param(s?) body | |
106 | 152 |
107 # X:template name(params) = "match" { | 153 # X:template name(params) = "match" { |
108 # X:template name( bar="init", baz={markup} ) = "match" mode="some" { | 154 # X:template name( bar="init", baz={markup} ) = "match" mode="some" { |
109 | 155 |
110 xtemplate : "X:template" param_name(?) xtemplate_params(?) | 156 xtemplate : name(?) params ( "=" value )(?) |
111 xtemplate_match(?) param(s?) body | 157 attrs body |
112 xtemplate_params: "(" xtemplate_param ("," xtemplate_param)(s?) ")" | |
113 xtemplate_param : param_name ("=" param_value)(?) | |
114 xtemplate_match : "=" param_value | |
115 | 158 |
116 # X:var LINK = "/article/@link"; | 159 # X:var LINK = "/article/@link"; |
117 # X:var year = { ... } | 160 # X:var year = { ... } |
118 | 161 # semicolon is optional |
119 xvar : xvar_name <commit> xvar_params | 162 |
120 xvar_params : param_name "=" param_value ";" | 163 xvariable : name "=" value(?) attrs body |
121 | param_name "=" body | 164 | name "=" value(?) |
122 | <error> | 165 | <error> |
123 | 166 |
124 xvar_name : "X:variable" | 167 # X:param XML = "'../xml'"; |
125 | "X:var" | 168 # X:param YEAR; |
126 | 169 |
127 # normal instructions | 170 xparam : name ("=" value)(?) attrs body |
128 | 171 |
129 xstylesheet : "X:stylesheet" | 172 # X:for-each "section[@id and @name]" { ... } |
130 xtransform : "X:transform" | 173 # X:for-each "link", X:sort "@id" { |
131 xattributeset : "X:attribute-set" | 174 |
132 xattribute : "X:attribute" | 175 xforeach : value attrs body |
133 xelement : "X:element" | 176 | value attrs "," "X:sort" <commit> value attrs body |
134 xvariable : "X:variable" | 177 |
135 xvar : "X:var" | 178 # X:sort select |
136 xparam : "X:param" | 179 # X:sort "@id" |
137 xapplytemplates : "X:apply-templates" | 180 |
138 xforeach : "X:for-each" | 181 xsort : value attrs body |
139 xchoose : "X:choose" | 182 |
140 xwhen : "X:when" | 183 # X:when "position() = 1" { ... } |
141 xotherwise : "X:otherwise" | 184 |
142 xvalueof : "X:value-of" | 185 xwhen : value attrs body |
143 xapplyimports : "X:apply-imports" | 186 |
144 xnumber : "X:number" | 187 # X:attribute "href" { ... } |
145 xoutput : "X:output" | 188 |
146 xinclude : "X:include" | 189 xattribute : value attrs body |
147 ximport : "X:import" | 190 |
148 xstripspace : "X:strip-space" | 191 # X:output |
149 xpreservespace : "X:preserve-space" | 192 # semicolon is optional |
150 xcopyof : "X:copy-of" | 193 |
151 xcopy : "X:copy" | 194 xoutput : attrs body |
152 xtext : "X:text" | 195 | attrs |
153 xsort : "X:sort" | 196 |
154 xcomment : "X:comment" | 197 # "X:copy-of" |
155 xprocessinginstruction : "X:processing-instruction" | 198 # semicolon is optional |
156 xdecimalformat : "X:decimal-format" | 199 |
157 xnamespacealias : "X:namespace-alias" | 200 xcopyof : value attrs body |
158 xkey : "X:key" | 201 | value |
159 xfallback : "X:fallback" | 202 |
160 xmessage : "X:message" | 203 # eof |
161 | 204 |
162 eofile : /^\Z/ | 205 eofile : /^\Z/ |
163 | 206 |
164 EOF | 207 EOF |
165 | 208 |