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

Last change on this file since 14481 was 14481, checked in by Don-vip, 5 months ago

fix #17053 - ignore MapCSS declarations starting with "-" to allow external extensions (used by osmose)

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