comparison xsls.pl @ 1:f082f8c2ebb0

XSLScript: some preliminary grammar, incomplete.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 19 Feb 2014 22:44:08 +0400
parents
children bcd96c403898
comparison
equal deleted inserted replaced
0:c9be645cc395 1:f082f8c2ebb0
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Convert from XSLScript to XSLT.
6 #
7 # Originally XSLScript was written by Paul Tchistopolskii. It is believed
8 # to be mostly identical to XSLT, but uses shorter syntax. Original
9 # implementation has major Java dependency, no longer supported and hard
10 # to find.
11 #
12 # This code doesn't pretend to be a full replacement, but rather an attempt
13 # to provide functionality needed for nginx documentation.
14
15 ###############################################################################
16
17 use warnings;
18 use strict;
19
20 use Parse::RecDescent;
21 use Getopt::Long;
22 use Data::Dumper qw/Dumper/;
23
24 ###############################################################################
25
26 GetOptions(
27 "trace!" => \$::RD_TRACE,
28 "hint!" => \$::RD_HINT,
29 )
30 or die "oops\n";
31
32 ###############################################################################
33
34 my $grammar = <<'EOF';
35
36 # XSLTScript grammar, reconstructed
37
38 startrule : item(s) eofile
39 { $return = $item[1] }
40
41 #item : "<!--" <commit> comment
42 # | "!" <commit> shortcut
43 # | "<%" <commit> instruction "%>"
44 # | instruction_name <commit>
45
46 item : comment | instruction | shortcut
47 item_text : comment | instruction | shortcut | text
48
49 # comments, <!-- ... -->
50 # not sure if it's something to be interpreted specially
51 # likely an artifact of our dump process
52
53 comment : "<!--" <commit> /((?!-->).)*/ms "-->"
54 { $return = ""; 1; }
55
56 # special chars: ', ", {, }, \
57 # if used in text, they needs to be escaped with backslash
58
59 text : quoted | unreserved | "'" | "\""
60 quoted : "\\" special
61 { $return = $item{special}; 1; }
62 special : "'" | "\"" | "\\" | "{" | "}"
63 unreserved : /[^'"\\{}]/
64
65 # shortcuts:
66 #
67 # !{xpath-expression} for X:value-of select="xpath-expression";
68 # !! for X:apply-templates
69 # !foo() for X:call-template name="foo"
70
71 shortcut : double_exclam | exclam_xpath | exclam_name
72 double_exclam : "!!" <commit> param(s?) ";"
73 exclam_xpath : "!{" <commit> text(s) "}"
74 exclam_name : "!" <commit> name "(" param(s?) ")"
75 name : /[a-z0-9_:-]+/i
76
77 param : param_name "=" param_value
78 param_name : /[a-z0-9_:-]+/i
79 param_value : "\"" /[^"]*/ "\""
80
81 body : ";"
82 | "{" <commit> item_text(s?) "}"
83
84 instructions : xstylesheet | xtransform
85 | xattributeset | xattribute | xelement
86 | xparam | xapplytemplates
87 | xforeach | xchoose | xwhen | xotherwise
88 | xvalueof | xapplyimports | xnumber
89 | xoutput | xinclude | ximport | xstripspace | xpreservespace
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
107 # X:template name(params) = "match" {
108 # X:template name( bar="init", baz={markup} ) = "match" mode="some" {
109
110 xtemplate : "X:template" param_name(?) xtemplate_params(?)
111 xtemplate_match(?) param(s?) body
112 xtemplate_params: "(" xtemplate_param ("," xtemplate_param)(s?) ")"
113 xtemplate_param : param_name ("=" param_value)(?)
114 xtemplate_match : "=" param_value
115
116 # X:var LINK = "/article/@link";
117 # X:var year = { ... }
118
119 xvar : xvar_name <commit> xvar_params
120 xvar_params : param_name "=" param_value ";"
121 | param_name "=" body
122 | <error>
123
124 xvar_name : "X:variable"
125 | "X:var"
126
127 # normal instructions
128
129 xstylesheet : "X:stylesheet"
130 xtransform : "X:transform"
131 xattributeset : "X:attribute-set"
132 xattribute : "X:attribute"
133 xelement : "X:element"
134 xvariable : "X:variable"
135 xvar : "X:var"
136 xparam : "X:param"
137 xapplytemplates : "X:apply-templates"
138 xforeach : "X:for-each"
139 xchoose : "X:choose"
140 xwhen : "X:when"
141 xotherwise : "X:otherwise"
142 xvalueof : "X:value-of"
143 xapplyimports : "X:apply-imports"
144 xnumber : "X:number"
145 xoutput : "X:output"
146 xinclude : "X:include"
147 ximport : "X:import"
148 xstripspace : "X:strip-space"
149 xpreservespace : "X:preserve-space"
150 xcopyof : "X:copy-of"
151 xcopy : "X:copy"
152 xtext : "X:text"
153 xsort : "X:sort"
154 xcomment : "X:comment"
155 xprocessinginstruction : "X:processing-instruction"
156 xdecimalformat : "X:decimal-format"
157 xnamespacealias : "X:namespace-alias"
158 xkey : "X:key"
159 xfallback : "X:fallback"
160 xmessage : "X:message"
161
162 eofile : /^\Z/
163
164 EOF
165
166 ###############################################################################
167
168 my $parser = Parse::RecDescent->new($grammar)
169 or die "Failed to create parser.\n";
170
171 my $lines;
172
173 {
174 local $/;
175 $lines = <>;
176 }
177
178 my $tree = $parser->startrule($lines)
179 or die "Failed to parse $ARGV.\n";
180
181 print Dumper($tree);
182
183 ###############################################################################
184 ###############################################################################