annotate src/luan/modules/parsers/Theme.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents 88b5b81cad4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package luan.modules.parsers;
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import luan.LuanException;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
4 import goodjava.parser.Parser;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
5 import goodjava.parser.ParseException;
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public final class Theme {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public static String toLuan(String source) throws LuanException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 try {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 return new Theme(source).parse();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 } catch(ParseException e) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 throw new LuanException(e.getMessage());
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 private final Parser parser;
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 private Theme(String source) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this.parser = new Parser(source);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 private ParseException exception(String msg) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 // parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return new ParseException(parser,msg);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 private String parse() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 StringBuilder stmts = new StringBuilder();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 stmts.append( "local M = {}; " );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 while( !parser.endOfInput() ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 String def = parseDef();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 if( def != null ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 stmts.append(def);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 } else {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 // parser.anyChar();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 stmts.append(parsePadding());
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 stmts.append( "\n\nreturn M\n" );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 return stmts.toString();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 private String parsePadding() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 int start = parser.currentIndex();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if( parser.match("--") ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 while( parser.noneOf("\r\n") );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 } else if( !parser.anyOf(" \t\r\n") ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 throw exception("unexpected text");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return parser.textFrom(start);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 private String parseDef() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 int start = parser.begin();
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
57 if( !parser.match('{') )
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
58 return parser.failure(null);
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
59 spaces();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
60 if( !parser.match("define:") )
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return parser.failure(null);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 String name = parseName();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 if( name==null )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 throw exception("invalid block name");
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
65 spaces();
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 if( !parser.match('}') )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 throw exception("unclosed define tag");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 String block = parseBody("define:"+name);
688
f99f51bc5bea fix up-values
Franklin Schmidt <fschmidt@gmail.com>
parents: 687
diff changeset
69 String rtn = "function M." + name + "(env) " + block + " end; ";
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 return parser.success(rtn);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 private String parseBody(String tagName) throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 StringBuilder stmts = new StringBuilder();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 int start = parser.currentIndex();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 int end = start;
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 while( !matchEndTag(tagName) ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 if( parser.endOfInput() ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 throw exception("unclosed block");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 String block = parseBlock();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 if( block != null ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 addText(start,end,stmts);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 start = parser.currentIndex();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 stmts.append(block);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 continue;
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 String simpleTag = parseSimpleTag();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 if( simpleTag != null ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 addText(start,end,stmts);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 start = parser.currentIndex();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 stmts.append(simpleTag);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 continue;
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 }
690
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
96 if( parser.match("<%") ) {
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
97 addText(start,end,stmts);
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
98 start = parser.currentIndex();
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
99 stmts.append("%><%='<%'%><%");
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
100 continue;
6a489a6b3cbc fix theme security
Franklin Schmidt <fschmidt@gmail.com>
parents: 688
diff changeset
101 }
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 parser.anyChar();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 end = parser.currentIndex();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 addText(start,end,stmts);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return stmts.toString();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 private boolean matchEndTag(String tagName) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 parser.begin();
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
111 if( !parser.match('{') )
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
112 return parser.failure();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
113 spaces();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
114 if( !(parser.match('/') && parser.match(tagName)) )
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
115 return parser.failure();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
116 spaces();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
117 if( !parser.match('}') )
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
118 return parser.failure();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
119 return parser.success();
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 private void addText(int start,int end,StringBuilder stmts) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 if( start < end ) {
688
f99f51bc5bea fix up-values
Franklin Schmidt <fschmidt@gmail.com>
parents: 687
diff changeset
124 stmts.append( "%>" ).append( parser.text.substring(start,end) ).append( "<%" );
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 private String parseBlock() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 int start = parser.begin();
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
130 if( !parser.match('{') )
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
131 return parser.failure(null);
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
132 spaces();
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
133 if( !parser.match("block:") )
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 return parser.failure(null);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 String name = parseName();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 if( name==null ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 throw exception("invalid block name");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 }
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
140 spaces();
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 if( !parser.match('}') )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 return parser.failure(null);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 String block = parseBody("block:"+name);
688
f99f51bc5bea fix up-values
Franklin Schmidt <fschmidt@gmail.com>
parents: 687
diff changeset
144 String rtn = " env."+ name + "( env, function(env) " + block + "end); ";
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 // String rtn = "<% env." + tag.name + "(" + (tag.attrs.isEmpty() ? "nil" : table(tag.attrs)) + ",env,function(env) %>" + block + "<% end) %>";
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 return parser.success(rtn);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 private String parseSimpleTag() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 int start = parser.begin();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 if( !parser.match('{') )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 return parser.failure(null);
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
153 spaces();
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 String name = parseName();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 if( name==null )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 return parser.failure(null);
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
157 spaces();
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 if( !parser.match('}') )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return parser.failure(null);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 // rtn = "<% env." + name + (attrs.isEmpty() ? "()" : table(attrs)) + " %>";
688
f99f51bc5bea fix up-values
Franklin Schmidt <fschmidt@gmail.com>
parents: 687
diff changeset
161 String rtn = " env." + name + "(env); ";
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 return parser.success(rtn);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 private boolean BlankLine() {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 parser.begin();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 while( parser.anyOf(" \t") );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 return EndOfLine() ? parser.success() : parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 private boolean EndOfLine() {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 return parser.match( "\r\n" ) || parser.match( '\r' ) || parser.match( '\n' );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 private String parseName() throws ParseException {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 int start = parser.begin();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 if( parser.match('/') ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 throw exception("bad closing tag");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 if( parser.match("define:") ) {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 parser.failure();
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 throw exception("unexpected definition");
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 if( !FirstNameChar() )
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 return parser.failure(null);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 while( NameChar() );
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 String match = parser.textFrom(start);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 return parser.success(match);
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 private boolean FirstNameChar() {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 return parser.inCharRange('a', 'z') || parser.inCharRange('A', 'Z') || parser.match('_');
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 private boolean NameChar() {
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 return FirstNameChar() || parser.inCharRange('0', '9');
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 }
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199
769
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
200 private void spaces() {
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
201 while( parser.anyOf(" \t") );
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
202 }
c5f5b655f1f7 allow spaces in theme tags
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
203
687
fc08c3b42010 add theme_to_luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 }