Ignore:
Timestamp:
2014-03-02T16:23:58+01:00 (10 years ago)
Author:
bastiK
Message:

mapcss: add support for @media expressions
includes part of the style sheet under certain condition
e.g. min josm version.

There is a major limitation: the intire file is still parsed.
So if new syntax is introduced, it will still generate
a parsing error for old josm version even if the new code
is wrapped in an @media section.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r6830 r6896  
    4141
    4242<DEFAULT>
     43TOKEN [IGNORE_CASE]:
     44{
     45 /* Special keywords in some contexts, ordinary identifiers in other contexts.
     46    Don't use the javacc token contexts (except DEFAULT and COMMENT) because
     47    they complicate error handling. Instead simply add a parsing rule <code>ident()</code>
     48    that parses the IDENT token and in addition all the keyword tokens. */
     49    < AND: "and" >
     50|   < SET: "set" >
     51|   < NOT: "not" >
     52|   < MEDIA: "@media" >
     53}
     54
     55<DEFAULT>
    4356TOKEN:
    4457{
    45     < SET: ("set" | "SET") >
    46 |   < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
     58    < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
    4759|   < UINT: ["1"-"9"] ( ["0"-"9"] )* >
    4860|   < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
     
    122134 * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
    123135 *
     136 * @media { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
     137 *
     138 *                               media_query
     139 *         ___________________________|_______________________________
     140 *        |                                                           |
     141 * @media all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
     142 *                |______________________|
     143 *                          |
     144 *                    media_expression
    124145 */
    125146
     
    166187}
    167188
     189String ident():
     190{
     191    Token t;
     192    String s;
     193}
     194{
     195    ( t=<IDENT> | t=<SET> | t=<NOT> | t=<AND> ) { return t.image; }
     196}
     197
    168198String string_or_ident() :
    169199{
     
    172202}
    173203{
    174     t=<IDENT> { return t.image; } | s=string() { return s; }
     204    ( s=ident() | s=string() ) { return s; }
    175205}
    176206
     
    229259{
    230260    MapCSSRule r;
    231     Token com = null;
    232261}
    233262{
    234263    { this.sheet = sheet; }
    235264    w()
     265    rules(true) ( media() w() rules(true) )*
     266    <EOF>
     267}
     268
     269void media():
     270{
     271    boolean pass = false;
     272    boolean q;
     273    boolean empty = true;
     274}
     275{
     276    <MEDIA> w()
     277    ( q=media_query() { pass = pass || q; empty = false; }
     278        ( <COMMA> w() q=media_query() { pass = pass || q; } )*
     279    )?
     280    <LBRACE> w()
     281    rules(empty || pass)
     282    <RBRACE>
     283}
     284
     285boolean media_query():
     286{
     287    Token t;
     288    String mediatype = "all";
     289    boolean pass = true;
     290    boolean invert = false;
     291    boolean e;
     292}
     293{
     294    ( <NOT> { invert = true; } w() )?
     295    (
     296            t=<IDENT> { mediatype = t.image.toLowerCase(); } w()
     297            ( <AND> w() e=media_expression() { pass = pass && e; } w() )*
     298        |
     299            e=media_expression() { pass = pass && e; } w()
     300            ( <AND> w() e=media_expression() { pass = pass && e; } w() )*
     301    )
     302    {
     303        if (!"all".equals(mediatype)) {
     304            pass = false;
     305        }
     306        return invert ? (!pass) : pass;
     307    }
     308}
     309
     310boolean media_expression():
     311{
     312    Token t;
     313    String feature;
     314    Object val = null;
     315}
     316{
     317    <LPAR> w() t=<IDENT> { feature = t.image; } w() ( <COLON> w() val=literal() )? <RPAR>
     318    { return this.sheet.evalMediaExpression(feature, val); }
     319}
     320
     321void rules(boolean add):
     322{
     323    MapCSSRule r;
     324}
     325{
    236326    (
    237327        try {
    238                 r=rule() { if (r != null) { sheet.rules.add(r); } } w()
     328            r=rule() { if (add && r != null) { sheet.rules.add(r); } } w()
    239329        } catch (MapCSSException mex) {
    240330            error_skipto(RBRACE, mex);
     
    245335        }
    246336    )*
    247     <EOF>
    248337}
    249338
     
    345434String tag_key() :
    346435{
    347     String s;
     436    String s, s2;
    348437    Token t;
    349438}
     
    351440        s=string() { return s; }
    352441    |
    353         t=<IDENT> { s = t.image; } ( <COLON> t=<IDENT> { s += ':' + t.image; } )* { return s; }
     442        s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
    354443}
    355444
     
    435524Condition class_or_pseudoclass(Context context) :
    436525{
    437     Token t;
     526    String s;
    438527    boolean not = false;
    439528    boolean pseudo;
     
    446535        <COLON> { pseudo = true; }
    447536    )
    448     t=<IDENT>
     537    s=ident()
    449538    { return pseudo
    450         ? Condition.createPseudoClassCondition(t.image, not, context)
    451         : Condition.createClassCondition(t.image, not, context); }
     539        ? Condition.createPseudoClassCondition(s, not, context)
     540        : Condition.createClassCondition(s, not, context); }
    452541}
    453542
    454543String subpart() :
    455544{
    456     Token t;
     545    String s;
    457546}
    458547{
    459548    <DCOLON>
    460     ( t=<IDENT> | t=<STAR> )
    461     { return t.image; }
     549    ( s=ident() { return s; } | <STAR> { return "*"; } )
    462550}
    463551
     
    573661}
    574662{
    575     tmp=<IDENT> { name = tmp.image; } w()
     663    name=ident() w()
    576664    <LPAR> w()
    577665    (
Note: See TracChangeset for help on using the changeset viewer.