source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj @ 9958

Last change on this file since 9958 was 9958, checked in by bastiK, 4 years ago

fix svn:eol property

  • Property svn:eol-style set to native
File size: 30.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2options {
3  STATIC = false;
4  OUTPUT_DIRECTORY = "parsergen";
5}
6
7PARSER_BEGIN(MapCSSParser)
8package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
9
10import static org.openstreetmap.josm.tools.I18n.tr;
11
12import java.io.InputStream;
13import java.io.Reader;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.Collections;
17import java.util.List;
18import java.util.Locale;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.gui.mappaint.Keyword;
22import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
23import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
24import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
25import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
26import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.NullExpression;
27import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
28import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
29import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
30import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
31import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
32import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
33import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
34import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
35import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
36import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
37import org.openstreetmap.josm.gui.mappaint.mapcss.Subpart;
38import org.openstreetmap.josm.tools.ColorHelper;
39import org.openstreetmap.josm.tools.Pair;
40import org.openstreetmap.josm.tools.Utils;
41
42/**
43 * MapCSS parser.
44 *
45 * Contains two independent grammars:
46 * (a) the preprocessor and (b) the main mapcss parser.
47 *
48 * The preprocessor handles @supports and @media syntax (@media is deprecated).
49 * Basically this allows to write one style for different versions of JOSM (or different editors).
50 * When the @supports condition is not fulfilled, it should simply skip over
51 * the whole section and not attempt to parse the possibly unknown
52 * grammar. It preserves whitespace and comments, in order to keep the
53 * line and column numbers in the error messages correct for the second pass.
54 *
55 */
56public class MapCSSParser {
57    MapCSSStyleSource sheet;
58    StringBuilder sb;
59    int declarationCounter;
60
61    /**
62     * Nicer way to refer to a lexical state.
63     */
64    public static enum LexicalState {
65        PREPROCESSOR(0), /* the preprocessor */
66        DEFAULT(2);      /* the main parser */
67       
68        int idx; // the integer, which javacc assigns to this state
69       
70        LexicalState(int idx) {
71            if (!this.name().equals(MapCSSParserTokenManager.lexStateNames[idx])) {
72                throw new RuntimeException();
73            }
74            this.idx = idx;
75        }
76    };
77   
78    /**
79     * Constructor which initializes the parser with a certain lexical state.
80     * @param in input
81     * @param encoding contents encoding
82     * @param initState initial state
83     */
84    public MapCSSParser(InputStream in, String encoding, LexicalState initState) {
85        this(createTokenManager(in, encoding, initState));
86        declarationCounter = 0;
87    }
88
89    protected static MapCSSParserTokenManager createTokenManager(InputStream in, String encoding, LexicalState initState) {
90        SimpleCharStream scs;
91        try {
92            scs = new SimpleCharStream(in, encoding, 1, 1);
93        } catch (java.io.UnsupportedEncodingException e) {
94            throw new RuntimeException(e);
95        }
96        return new MapCSSParserTokenManager(scs, initState.idx);
97    }
98
99    /**
100     * Constructor which initializes the parser with a certain lexical state.
101     * @param in input
102     * @param initState initial state
103     */
104    public MapCSSParser(Reader in, LexicalState initState) {
105        this(createTokenManager(in, initState));
106        declarationCounter = 0;
107    }
108
109    protected static MapCSSParserTokenManager createTokenManager(Reader in, LexicalState initState) {
110        final SimpleCharStream scs = new SimpleCharStream(in, 1, 1);
111        return new MapCSSParserTokenManager(scs, initState.idx);
112    }
113}
114PARSER_END(MapCSSParser)
115
116/**
117 * Token definitions
118 *
119 * Lexical states for the preprocessor: <PREPROCESSOR>, <PP_COMMENT>
120 * Lexical states for the main parser: <DEFAULT>, <COMMENT>
121 */
122 
123<PREPROCESSOR>
124TOKEN:
125{
126    < PP_AND: "and" >
127|   < PP_OR: "or" >
128|   < PP_NOT: "not" >
129|   < PP_SUPPORTS: "@supports" >
130|   < PP_MEDIA: "@media" >
131|   < PP_NEWLINECHAR: "\n" | "\r" | "\f" >
132|   < PP_WHITESPACE: " " | "\t" >
133|   < PP_COMMENT_START: "/*" > : PP_COMMENT
134}
135
136<PP_COMMENT>
137TOKEN:
138{
139    < PP_COMMENT_END: "*/" > : PREPROCESSOR
140}
141
142<PP_COMMENT>
143MORE:
144{
145    < ~[] >
146}
147
148<DEFAULT>
149TOKEN [IGNORE_CASE]:
150{
151 /* Special keyword in some contexts, ordinary identifier in other contexts.
152    Use the parsing rule <code>ident()</code> to refer to a general
153    identifier, including "set". */
154   < SET: "set" >
155}
156
157<DEFAULT,PREPROCESSOR>
158TOKEN:
159{
160    < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
161|   < UINT: ( ["0"-"9"] )+ >
162|   < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )*  "\"" >
163|   < #PREDEFINED: "\\" ["d","D","s","S","w","W","b","B","A","G","Z","z"] >
164|   < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" |  "\\(" | "\\)" | "\\{" | "\\}" | "\\?" | "\\*" | "\\^" | "\\$" | "\\|" | "\\p" |<PREDEFINED> >
165|   < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )*  "/" >
166|   < LBRACE: "{" >
167|   < RBRACE: "}" >
168|   < LPAR: "(" >
169|   < RPAR: ")" >
170|   < COMMA: "," >
171|   < COLON: ":" >
172}
173
174<PREPROCESSOR>
175TOKEN:
176{
177    < PP_SOMETHING_ELSE : ~[] >
178}
179
180<DEFAULT>
181TOKEN:
182{
183    < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
184|   < #H: ["0"-"9","a"-"f","A"-"F"] >
185|   < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
186|   < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
187|   < STAR: "*" >
188|   < SLASH: "/" >
189|   < LSQUARE: "[" >
190|   < RSQUARE: "]" >
191|   < GREATER_EQUAL: ">=" >
192|   < LESS_EQUAL: "<=" >
193|   < GREATER: ">" >
194|   < LESS: "<" >
195|   < EQUAL: "=" >
196|   < EXCLAMATION: "!" >
197|   < TILDE: "~" >
198|   < DCOLON: "::" >
199|   < SEMICOLON: ";" >
200|   < PIPE: "|" >
201|   < PIPE_Z: "|z" >
202|   < PLUS: "+" >
203|   < MINUS: "-" >
204|   < AMPERSAND: "&" >
205|   < QUESTION: "?" >
206|   < DOLLAR: "$" >
207|   < CARET: "^" >
208|   < FULLSTOP: "." >
209|   < DEG: "°" >
210|   < ELEMENT_OF: "∈" >
211|   < CROSSING: "⧉" >
212|   < PERCENT: "%" >
213|   < COMMENT_START: "/*" > : COMMENT
214|   < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
215}
216
217<COMMENT>
218TOKEN:
219{
220    < COMMENT_END: "*/" > : DEFAULT
221}
222
223<COMMENT>
224SKIP:
225{
226    < ~[] >
227}
228
229
230/*
231 * Preprocessor parser definitions:
232 *
233 * <pre>
234 *
235 * {@literal @media} { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
236 *
237 *                               media_query
238 *         ___________________________|_______________________________
239 *        |                                                           |
240 * {@literal @media} all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
241 *                |______________________|
242 *                          |
243 *                    media_expression
244 * </pre>
245 */
246 
247
248/**
249 * root method for the preprocessor.
250 * @param sheet MapCSS style source
251 * @return result string
252 * @throws ParseException in case of parsing error
253 */
254String pp_root(MapCSSStyleSource sheet):
255{
256}
257{
258    { sb = new StringBuilder(); this.sheet = sheet; }
259    pp_black_box(true) <EOF>
260    { return sb.toString(); }
261}
262
263/**
264 * Parse any unknown grammar (black box).
265 *
266 * Only stop when "@media" is encountered and keep track of correct number of
267 * opening and closing curly brackets.
268 *
269 * @param write false if this content should be skipped (@pp_media condition is not fulfilled), true otherwise
270 * @throws ParseException in case of parsing error
271 */
272void pp_black_box(boolean write):
273{
274    Token t;
275}
276{
277    (
278        (t=<PP_AND> | t=<PP_OR> | t=<PP_NOT> | t=<UINT> | t=<STRING> | t=<REGEX> | t=<LPAR> | t=<RPAR> | t=<COMMA> | t=<COLON> | t=<IDENT> | t=<PP_SOMETHING_ELSE>) { if (write) sb.append(t.image); }
279        |
280            pp_w1()
281        |
282            pp_supports(!write)
283        |
284            pp_media(!write)
285        |
286            t=<LBRACE> { if (write) sb.append(t.image); } pp_black_box(write) t=<RBRACE> { if (write) sb.append(t.image); }
287    )*
288}
289
290/**
291 * Parses an @supports rule.
292 *
293 * @param ignore if the content of this rule should be ignored
294 * (because we are already inside a @supports block that didn't pass)
295 * @throws ParseException in case of parsing error
296 */
297void pp_supports(boolean ignore):
298{
299    boolean pass;
300}
301{
302    <PP_SUPPORTS> pp_w()
303    pass=pp_supports_condition()
304    <LBRACE>
305    pp_black_box(pass && !ignore)
306    <RBRACE>
307}
308
309/**
310 * Parses the condition of the @supports rule.
311 *
312 * Unlike other parsing rules, grabs trailing whitespace.
313 * @return true, if the condition is fulfilled
314 * @throws ParseException in case of parsing error
315 */
316boolean pp_supports_condition():
317{
318    boolean pass;
319    boolean q;
320}
321{
322    (
323        <PP_NOT> pp_w() q=pp_supports_condition_in_parens() { pass = !q; } pp_w()
324    |
325        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_AND>)
326        pass=pp_supports_condition_in_parens() pp_w()
327        ( <PP_AND> pp_w() q=pp_supports_condition_in_parens() { pass = pass && q; } pp_w() )+
328    |
329        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_OR>)
330        pass=pp_supports_condition_in_parens() pp_w()
331        ( <PP_OR> pp_w() q=pp_supports_condition_in_parens() { pass = pass || q; } pp_w() )+
332    |
333        pass=pp_supports_condition_in_parens() pp_w()
334    )
335    { return pass; }
336}
337
338/**
339 * Parses something in parenthesis inside the condition of the @supports rule.
340 *
341 * @return true, if the condition is fulfilled
342 * @throws ParseException in case of parsing error
343 */
344boolean pp_supports_condition_in_parens():
345{
346    boolean pass;
347}
348{
349    (
350        LOOKAHEAD(pp_supports_declaration_condition())
351        pass=pp_supports_declaration_condition()
352    |
353        <LPAR> pp_w() pass=pp_supports_condition() <RPAR>
354    )
355    { return pass; }
356}
357
358/**
359 * Parse an @supports declaration condition, e.&nbsp;g. a single (key:value) or (key) statement.
360 *
361 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
362 *
363 * @return true if the condition is fulfilled
364 * @throws ParseException in case of parsing error
365 */
366boolean pp_supports_declaration_condition():
367{
368    Token t;
369    String feature;
370    Object val = null;
371}
372{
373    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
374    { return this.sheet.evalSupportsDeclCondition(feature, val); }
375}
376
377// deprecated
378void pp_media(boolean ignore):
379{
380    boolean pass = false;
381    boolean q;
382    boolean empty = true;
383}
384{
385    {
386        if (sheet != null) {
387            String msg = tr("Detected deprecated ''{0}'' in ''{1}'' which will be removed shortly. Use ''{2}'' instead.",
388                            "@media", sheet.getDisplayString(), "@supports");
389            Main.error(msg);
390            sheet.logWarning(msg);
391        }
392    }
393    <PP_MEDIA> pp_w()
394    ( q=pp_media_query() { pass = pass || q; empty = false; }
395        ( <COMMA> pp_w() q=pp_media_query() { pass = pass || q; } )*
396    )?
397    <LBRACE>
398    pp_black_box((empty || pass) && !ignore)
399    <RBRACE>
400}
401
402// deprecated
403boolean pp_media_query():
404{
405    Token t;
406    String mediatype = "all";
407    boolean pass = true;
408    boolean invert = false;
409    boolean e;
410}
411{
412    ( <PP_NOT> { invert = true; } pp_w() )?
413    (
414            t=<IDENT> { mediatype = t.image.toLowerCase(Locale.ENGLISH); } pp_w()
415            ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
416        |
417            e=pp_media_expression() { pass = pass && e; } pp_w()
418            ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
419    )
420    {
421        if (!"all".equals(mediatype)) {
422            pass = false;
423        }
424        return invert ? (!pass) : pass;
425    }
426}
427
428/**
429 * Parse an @media expression.
430 *
431 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
432 *
433 * @return true if the condition is fulfilled
434 * @throws ParseException in case of parsing error
435 */
436// deprecated
437boolean pp_media_expression():
438{
439    Token t;
440    String feature;
441    Object val = null;
442}
443{
444    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
445    { return this.sheet.evalSupportsDeclCondition(feature, val); }
446}
447
448void pp_w1():
449{
450    Token t;
451}
452{
453    t=<PP_NEWLINECHAR> { sb.append(t.image); }
454        |
455    t=<PP_WHITESPACE> { sb.append(t.image); }
456        |
457    t=<PP_COMMENT_START> { sb.append(t.image); } t=<PP_COMMENT_END> { sb.append(t.image); }
458}
459
460void pp_w():
461{
462}
463{
464 ( pp_w1() )*
465}
466
467/*
468 * Parser definition for the main MapCSS parser:
469 *
470 * <pre>
471 *
472 *                       rule
473 *  _______________________|______________________________
474 * |                                                      |
475 *        selector                      declaration
476 *  _________|___________________   _________|____________
477 * |                             | |                      |
478 *
479 * way|z11-12[highway=residential] { color: red; width: 3 }
480 *
481 *    |_____||___________________|   |_________|
482 *       |            |                   |
483 *     zoom       condition          instruction
484 *
485 * more general:
486 *
487 * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
488 *
489 * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
490 *
491 * </pre>
492 */
493
494int uint() :
495{
496    Token i;
497}
498{
499    i=<UINT> { return Integer.parseInt(i.image); }
500}
501
502int int_() :
503{
504    int i;
505}
506{
507    <MINUS> i=uint() { return -i; } | i=uint() { return i; }
508}
509
510float ufloat() :
511{
512    Token f;
513}
514{
515    ( f=<UFLOAT> | f=<UINT> )
516    { return Float.parseFloat(f.image); }
517}
518
519float float_() :
520{
521    float f;
522}
523{
524    <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
525}
526
527String string() :
528{
529    Token t;
530}
531{
532    t=<STRING>
533    { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
534}
535
536String ident():
537{
538    Token t;
539    String s;
540}
541{
542    ( t=<IDENT> | t=<SET> ) { return t.image; }
543}
544
545String string_or_ident() :
546{
547    Token t;
548    String s;
549}
550{
551    ( s=ident() | s=string() ) { return s; }
552}
553
554String regex() :
555{
556    Token t;
557}
558{
559    t=<REGEX>
560    { return t.image.substring(1, t.image.length() - 1); }
561}
562
563/**
564 * white-space
565 * @throws ParseException in case of parsing error
566 */
567void s() :
568{
569}
570{
571    ( <S> )?
572}
573
574/**
575 * mix of white-space and comments
576 * @throws ParseException in case of parsing error
577 */
578void w() :
579{
580}
581{
582    ( <S> | <COMMENT_START> <COMMENT_END> )*
583}
584
585/**
586 * comma delimited list of floats (at least 2, all &gt;= 0)
587 * @return list of floats
588 * @throws ParseException in case of parsing error
589 */
590List<Float> float_array() :
591{
592    float f;
593    List<Float> fs = new ArrayList<Float>();
594}
595{
596    f=ufloat() { fs.add(f); }
597    (
598        <COMMA> s()
599        f=ufloat() { fs.add(f); }
600    )+
601    {
602        return fs;
603    }
604}
605
606/**
607 * entry point for the main parser
608 * @param sheet MapCSS style source
609 * @throws ParseException in case of parsing error
610 */
611void sheet(MapCSSStyleSource sheet):
612{
613}
614{
615    { this.sheet = sheet; }
616    w()
617    (
618        try {
619            rule() w()
620        } catch (MapCSSException mex) {
621            Main.error(mex);
622            error_skipto(RBRACE, mex);
623            w();
624        } catch (ParseException ex) {
625            error_skipto(RBRACE, null);
626            w();
627        }
628    )*
629    <EOF>
630}
631
632void rule():
633{
634    List<Selector> selectors;
635    Declaration decl;
636}
637{
638    selectors=selectors()
639    decl=declaration()
640    {
641        for (Selector s : selectors) {
642            sheet.rules.add(new MapCSSRule(s, decl));
643        }
644    }
645}
646
647List<Selector> selectors():
648{
649    List<Selector> selectors = new ArrayList<Selector>();
650    Selector sel;
651}
652{
653    sel=child_selector() { selectors.add(sel); }
654    (
655        <COMMA> w()
656        sel=child_selector() { selectors.add(sel); }
657    )*
658    { return selectors; }
659}
660
661Selector child_selector() :
662{
663    Selector.ChildOrParentSelectorType type = null;
664    Condition c;
665    List<Condition> conditions = new ArrayList<Condition>();
666    Selector selLeft;
667    LinkSelector selLink = null;
668    Selector selRight = null;
669}
670{
671    selLeft=selector() w()
672    (
673        (
674            (
675                (
676                    <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; }
677                |
678                    <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; }
679                |
680                    <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; }
681                )
682                ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )*
683            |
684                <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
685            |
686                <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
687            )
688            w()
689        |
690            { /* <GREATER> is optional for child selector */ type = Selector.ChildOrParentSelectorType.CHILD; }
691        )
692        { selLink = new LinkSelector(conditions); }
693        selRight=selector() w()
694    )?
695    { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
696}
697
698Selector selector() :
699{
700    Token base;
701    Condition c;
702    Pair<Integer, Integer> r = null;
703    List<Condition> conditions = new ArrayList<Condition>();
704    Subpart sub = null;
705}
706{
707    ( base=<IDENT> | base=<STAR> )
708    ( r=zoom() )?
709    ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
710    ( sub=subpart() )?
711    { return new GeneralSelector(base.image, r, conditions, sub); }
712}
713
714Pair<Integer, Integer> zoom() :
715{
716    Integer min = 0;
717    Integer max = Integer.MAX_VALUE;
718}
719{
720    <PIPE_Z>
721    (
722            <MINUS> max=uint()
723        |
724        LOOKAHEAD(2)
725            min=uint() <MINUS> ( max=uint() )?
726        |
727            min=uint() { max = min; }
728    )
729    { return new Pair<Integer, Integer>(min, max); }
730}
731
732Condition condition(Context context) :
733{
734    Condition c;
735    Expression e;
736}
737{
738    <LSQUARE> s()
739    (
740        LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
741            c=simple_key_condition(context) s() <RSQUARE> { return c; }
742        |
743        LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
744            c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
745        |
746            e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
747    )
748}
749
750String tag_key() :
751{
752    String s, s2;
753    Token t;
754}
755{
756        s=string() { return s; }
757    |
758        s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
759}
760
761Condition simple_key_condition(Context context) :
762{
763    boolean not = false;
764    Condition.KeyMatchType matchType = null;;
765    String key;
766}
767{
768    ( <EXCLAMATION> { not = true; } )?
769    (
770        { matchType = Condition.KeyMatchType.REGEX; } key = regex()
771    |
772        key = tag_key()
773    )
774    ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )?
775    (              <QUESTION>               { matchType = Condition.KeyMatchType.TRUE;  } )?
776    { return Condition.createKeyCondition(key, not, matchType, context); }
777}
778
779Condition simple_key_value_condition(Context context) :
780{
781    String key;
782    String val;
783    float f;
784    int i;
785    Condition.KeyMatchType matchType = null;;
786    Condition.Op op;
787    boolean considerValAsKey = false;
788}
789{
790    (
791        key = regex() s() { matchType = Condition.KeyMatchType.REGEX; }
792    |
793        key=tag_key() s()
794    )
795    (
796        LOOKAHEAD(3)
797            (
798                    <EQUAL> <TILDE> { op=Condition.Op.REGEX; }
799                |
800                    <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; }
801            )
802            s()
803            ( <STAR> { considerValAsKey=true; } )?
804            val=regex()
805        |
806            (
807                    <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
808                |
809                    <EQUAL> { op=Condition.Op.EQ; }
810                |
811                    <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
812                |
813                    <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
814                |
815                    <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
816                |
817                    <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
818            )
819            s()
820            ( <STAR> { considerValAsKey=true; } )?
821            (
822                LOOKAHEAD(2)
823                        i=int_() { val=Integer.toString(i); }
824                    |
825                        f=float_() { val=Float.toString(f); }
826                    |
827                        val=string_or_ident()
828            )
829        |
830            (
831                    <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
832                |
833                    <GREATER> { op=Condition.Op.GREATER; }
834                |
835                    <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
836                |
837                    <LESS> { op=Condition.Op.LESS; }
838            )
839            s()
840            f=float_() { val=Float.toString(f); }
841    )
842    { return Condition.KeyMatchType.REGEX == matchType
843            ? Condition.createRegexpKeyRegexpValueCondition(key, val, op)
844            : Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
845}
846
847Condition class_or_pseudoclass(Context context) :
848{
849    String s;
850    boolean not = false;
851    boolean pseudo;
852}
853{
854    ( <EXCLAMATION> { not = true; } )?
855    (
856        <FULLSTOP> { pseudo = false; }
857    |
858        <COLON> { pseudo = true; }
859    )
860    s=ident()
861    { return pseudo
862        ? Condition.createPseudoClassCondition(s, not, context)
863        : Condition.createClassCondition(s, not, context); }
864}
865
866Subpart subpart() :
867{
868    String s;
869    Expression e;
870}
871{
872    <DCOLON>
873    (
874        s=ident() { return new Subpart.StringSubpart(s); }
875    |
876        <STAR> { return new Subpart.StringSubpart("*"); }
877    |
878        <LPAR> e=expression() <RPAR> { return new Subpart.ExpressionSubpart(e); }
879    )
880}
881
882Declaration declaration() :
883{
884    List<Instruction> ins = new ArrayList<Instruction>();
885    Instruction i;
886    Token key;
887    Object val = null;
888}
889{
890    <LBRACE> w()
891    (
892        (
893            <SET> w()
894            (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class"
895            key=<IDENT> w()
896            ( <EQUAL> val=expression() )?
897            { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
898            ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
899        )
900    |
901        key=<IDENT> w() <COLON> w()
902        (
903            LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
904                val=float_array()
905                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
906                w()
907                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
908            |
909            LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
910                val=expression()
911                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
912                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
913            |
914                val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
915        )
916    )*
917    <RBRACE>
918    { return new Declaration(ins, declarationCounter++); }
919}
920
921/**
922 * General expression.
923 * Separate production rule for each level of operator precedence (recursive descent).
924 */
925Expression expression() :
926{
927    Expression e;
928}
929{
930    e=conditional_expression()
931    {
932        return e;
933    }
934}
935
936Expression conditional_expression() :
937{
938    Expression e, e1, e2;
939    String op = null;
940}
941{
942    e=or_expression()
943    (
944        <QUESTION> w()
945        e1=conditional_expression()
946        <COLON> w()
947        e2=conditional_expression()
948        {
949            e = ExpressionFactory.createFunctionExpression("cond", Arrays.asList(e, e1, e2));
950        }
951    )?
952    {
953        return e;
954    }
955}
956
957Expression or_expression() :
958{
959    Expression e, e2;
960    String op = null;
961}
962{
963    e=and_expression()
964    (
965        <PIPE> <PIPE> w()
966        e2=and_expression()
967        {
968            e = ExpressionFactory.createFunctionExpression("or", Arrays.asList(e, e2));
969        }
970    )*
971    {
972        return e;
973    }
974}
975
976Expression and_expression() :
977{
978    Expression e, e2;
979    String op = null;
980}
981{
982    e=relational_expression()
983    (
984        <AMPERSAND> <AMPERSAND> w()
985        e2=relational_expression()
986        {
987            e = ExpressionFactory.createFunctionExpression("and", Arrays.asList(e, e2));
988        }
989    )*
990    {
991        return e;
992    }
993}
994
995Expression relational_expression() :
996{
997    Expression e, e2;
998    String op = null;
999}
1000{
1001    e=additive_expression()
1002    (
1003        (
1004            <GREATER_EQUAL> { op = "greater_equal"; }
1005            |
1006            <LESS_EQUAL> { op = "less_equal"; }
1007            |
1008            <GREATER> { op = "greater"; }
1009            |
1010            <LESS> { op = "less"; }
1011            |
1012            <EQUAL> ( <EQUAL> )? { op = "equal"; }
1013            |
1014            <EXCLAMATION> <EQUAL> { op = "not_equal"; }
1015        ) w()
1016        e2=additive_expression()
1017        {
1018            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
1019        }
1020    )?
1021    {
1022        return e;
1023    }
1024}
1025
1026Expression additive_expression() :
1027{
1028    Expression e, e2;
1029    String op = null;
1030}
1031{
1032    e=multiplicative_expression()
1033    (
1034        ( <PLUS> { op = "plus"; } | <MINUS> { op = "minus"; } ) w()
1035        e2=multiplicative_expression()
1036        {
1037            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
1038        }
1039    )*
1040    {
1041        return e;
1042    }
1043}
1044
1045Expression multiplicative_expression() :
1046{
1047    Expression e, e2;
1048    String op = null;
1049}
1050{
1051    e=unary_expression()
1052    (
1053        ( <STAR> { op = "times"; } | <SLASH> { op = "divided_by"; } ) w()
1054        e2=unary_expression()
1055        {
1056            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
1057        }
1058    )*
1059    {
1060        return e;
1061    }
1062}
1063
1064Expression unary_expression() :
1065{
1066    Expression e;
1067    String op = null;
1068}
1069{
1070    (
1071        <MINUS> { op = "minus"; } w()
1072    |
1073        <EXCLAMATION> { op = "not"; } w()
1074    )?
1075    e=primary() w()
1076    {
1077        if (op == null)
1078            return e;
1079        return ExpressionFactory.createFunctionExpression(op, Collections.singletonList(e));
1080    }
1081}
1082
1083Expression primary() :
1084{
1085    Expression nested;
1086    Expression fn;
1087    Object lit;
1088}
1089{
1090    LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
1091        fn=function() { return fn; }
1092    |
1093        lit=literal()
1094        {
1095            if (lit == null)
1096                return NullExpression.INSTANCE;
1097            return new LiteralExpression(lit);
1098        }
1099    |
1100        <LPAR> w() nested=expression() <RPAR> { return nested; }
1101}
1102
1103Expression function() :
1104{
1105    Expression arg;
1106    String name;
1107    List<Expression> args = new ArrayList<Expression>();
1108}
1109{
1110    name=ident() w()
1111    <LPAR> w()
1112    (
1113        arg=expression() { args.add(arg); }
1114        ( <COMMA> w() arg=expression() { args.add(arg); } )*
1115    )?
1116    <RPAR>
1117    { return ExpressionFactory.createFunctionExpression(name, args); }
1118}
1119
1120Object literal() :
1121{
1122    String val, pref;
1123    Token t;
1124    Float f;
1125}
1126{
1127        LOOKAHEAD(2)
1128        pref=ident() t=<HEXCOLOR>
1129        { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
1130    |
1131        t=<IDENT> { return new Keyword(t.image); }
1132    |
1133        val=string() { return val; }
1134    |
1135        <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
1136    |
1137        LOOKAHEAD(2)
1138        f=ufloat_unit() { return f; }
1139    |
1140        f=ufloat() { return f; }
1141    |
1142        t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
1143}
1144
1145/**
1146 * Number followed by a unit.
1147 *
1148 * Returns angles in radians and lengths in pixels.
1149 */
1150Float ufloat_unit() :
1151{
1152    float f;
1153    String u;
1154}
1155{
1156    f=ufloat() ( u=ident() | <DEG> { u = "°"; } | <PERCENT> { u = "%"; } )
1157    {
1158        Double m = unit_factor(u);
1159        if (m == null)
1160            return null;
1161        return (float) (f * m);
1162    }
1163}
1164
1165JAVACODE
1166private Double unit_factor(String unit) {
1167    switch (unit) {
1168        case "deg":
1169        case "°": return Math.PI / 180;
1170        case "rad": return 1.;
1171        case "grad": return Math.PI / 200;
1172        case "turn": return 2 * Math.PI;
1173        case "%": return 0.01;
1174        case "px": return 1.;
1175        case "cm": return 96/2.54;
1176        case "mm": return 9.6/2.54;
1177        case "in": return 96.;
1178        case "q": return 2.4/2.54;
1179        case "pc": return 16.;
1180        case "pt": return 96./72;
1181        default: return null;
1182    }
1183}
1184
1185JAVACODE
1186void error_skipto(int kind, MapCSSException me) {
1187    if (token.kind == EOF)
1188        throw new ParseException("Reached end of file while parsing");
1189       
1190    Exception e = null;       
1191    ParseException pe = generateParseException();
1192
1193    if (me != null) {
1194        final Token token = Utils.firstNonNull(pe.currentToken.next, pe.currentToken);
1195        me.setLine(token.beginLine);
1196        me.setColumn(token.beginColumn);
1197        e = me;
1198    } else {
1199        e = new ParseException(pe.getMessage()); // prevent memory leak
1200    }
1201   
1202    Main.error("Skipping to the next rule, because of an error:");
1203    Main.error(e);
1204    if (sheet != null) {
1205        sheet.logError(e);
1206    }
1207    Token t;
1208    do {
1209        t = getNextToken();
1210    } while (t.kind != kind && t.kind != EOF);
1211    if (t.kind == EOF)
1212        throw new ParseException("Reached end of file while parsing");
1213}
1214
1215JAVACODE
1216/**
1217 * read everything to the next semicolon
1218 */
1219String readRaw() {
1220    Token t;
1221    StringBuilder s = new StringBuilder();
1222    while (true) {
1223        t = getNextToken();
1224        if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
1225                t.image.contains("\n")) {
1226            ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
1227            Main.error(e);
1228            if (sheet != null) {
1229                sheet.logError(e);
1230            }
1231        }
1232        if (t.kind == SEMICOLON || t.kind == EOF)
1233            break;
1234        s.append(t.image);
1235    }
1236    if (t.kind == EOF)
1237        throw new ParseException("Reached end of file while parsing");
1238    return s.toString();
1239}
1240
Note: See TracBrowser for help on using the repository browser.