// License: GPL. For details, see LICENSE file.
options {
  STATIC = false;
  OUTPUT_DIRECTORY = "parsergen";
}

PARSER_BEGIN(MapCSSParser)
package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;

import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.gui.mappaint.Keyword;
import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.NullExpression;
import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
import org.openstreetmap.josm.gui.mappaint.mapcss.Subpart;
import org.openstreetmap.josm.tools.ColorHelper;
import org.openstreetmap.josm.tools.Pair;

/**
 * MapCSS parser.
 *
 * Contains two independent grammars:
 * (a) the preprocessor and (b) the main mapcss parser.
 *
 * The preprocessor handles @supports and @media syntax (@media is deprecated).
 * Basically this allows to write one style for different versions of JOSM (or different editors).
 * When the @supports condition is not fulfilled, it should simply skip over
 * the whole section and not attempt to parse the possibly unknown
 * grammar. It preserves whitespace and comments, in order to keep the
 * line and column numbers in the error messages correct for the second pass.
 *
 */

public class MapCSSParser {
    MapCSSStyleSource sheet;
    StringBuilder sb;
    int declarationCounter;

    /**
     * Nicer way to refer to a lexical state.
     */
    public static enum LexicalState {
        PREPROCESSOR(0), /* the preprocessor */
        DEFAULT(2);      /* the main parser */
        
        int idx; // the integer, which javacc assigns to this state
        
        LexicalState(int idx) {
            if (!this.name().equals(MapCSSParserTokenManager.lexStateNames[idx])) {
                throw new RuntimeException();
            }
            this.idx = idx;
        }
    };
    
    /**
     * Constructor which initializes the parser with a certain lexical state.
     */
    public MapCSSParser(InputStream in, String encoding, LexicalState initState) {
        this(createTokenManager(in, encoding, initState));
        declarationCounter = 0;
    }

    protected static MapCSSParserTokenManager createTokenManager(InputStream in, String encoding, LexicalState initState) {
        SimpleCharStream scs;
        try {
            scs = new SimpleCharStream(in, encoding, 1, 1);
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return new MapCSSParserTokenManager(scs, initState.idx);
    }

    /**
     * Constructor which initializes the parser with a certain lexical state.
     */
    public MapCSSParser(Reader in, LexicalState initState) {
        this(createTokenManager(in, initState));
        declarationCounter = 0;
    }

    protected static MapCSSParserTokenManager createTokenManager(Reader in, LexicalState initState) {
        final SimpleCharStream scs = new SimpleCharStream(in, 1, 1);
        return new MapCSSParserTokenManager(scs, initState.idx);
    }
}
PARSER_END(MapCSSParser)

/*************
 * Token definitions
 *
 * Lexical states for the preprocessor: <PREPROCESSOR>, <PP_COMMENT>
 * Lexical states for the main parser: <DEFAULT>, <COMMENT>
 */
 
<PREPROCESSOR>
TOKEN:
{
    < PP_AND: "and" >
|   < PP_OR: "or" >
|   < PP_NOT: "not" >
|   < PP_SUPPORTS: "@supports" >
|   < PP_MEDIA: "@media" >
|   < PP_NEWLINECHAR: "\n" | "\r" | "\f" >
|   < PP_WHITESPACE: " " | "\t" >
|   < PP_COMMENT_START: "/*" > : PP_COMMENT
}

<PP_COMMENT>
TOKEN:
{
    < PP_COMMENT_END: "*/" > : PREPROCESSOR
}

<PP_COMMENT>
MORE:
{
    < ~[] >
}

<DEFAULT>
TOKEN [IGNORE_CASE]:
{
 /* Special keyword in some contexts, ordinary identifier in other contexts.
    Use the parsing rule <code>ident()</code> to refer to a general
    identifier, including "set". */
   < SET: "set" >
}

<DEFAULT,PREPROCESSOR>
TOKEN:
{
    < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
|   < UINT: ["1"-"9"] ( ["0"-"9"] )* >
|   < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )*  "\"" >
|   < #PREDEFINED: "\\" ["d","D","s","S","w","W","b","B","A","G","Z","z"] >
|   < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" | "\\[" | "\\]" | "\\+" | "\\." | "\\'" | "\\\"" |  "\\(" | "\\)" |<PREDEFINED> >
|   < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )*  "/" >
|   < LBRACE: "{" >
|   < RBRACE: "}" >
|   < LPAR: "(" >
|   < RPAR: ")" >
|   < COMMA: "," >
|   < COLON: ":" >
}

<PREPROCESSOR>
TOKEN:
{
    < PP_SOMETHING_ELSE : ~[] >
}

<DEFAULT>
TOKEN:
{
    < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
|   < #H: ["0"-"9","a"-"f","A"-"F"] >
|   < HEXCOLOR: "#" ( <H><H><H><H><H><H><H><H> | <H><H><H><H><H><H> | <H><H><H> ) >
|   < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
|   < STAR: "*" >
|   < SLASH: "/" >
|   < LSQUARE: "[" >
|   < RSQUARE: "]" >
|   < GREATER_EQUAL: ">=" >
|   < LESS_EQUAL: "<=" >
|   < GREATER: ">" >
|   < LESS: "<" >
|   < EQUAL: "=" >
|   < EXCLAMATION: "!" >
|   < TILDE: "~" >
|   < DCOLON: "::" >
|   < SEMICOLON: ";" >
|   < PIPE: "|" >
|   < PIPE_Z: "|z" >
|   < PLUS: "+" >
|   < MINUS: "-" >
|   < AMPERSAND: "&" >
|   < QUESTION: "?" >
|   < DOLLAR: "$" >
|   < CARET: "^" >
|   < FULLSTOP: "." >
|   < DEG: "°" >
|   < ELEMENT_OF: "∈" >
|   < CROSSING: "⧉" >
|   < COMMENT_START: "/*" > : COMMENT
|   < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
}

<COMMENT>
TOKEN:
{
    < COMMENT_END: "*/" > : DEFAULT
}

<COMMENT>
SKIP:
{
    < ~[] >
}


/*************
 *
 * Preprocessor parser definitions:
 *
 * <pre>
 *
 * {@literal @media} { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
 *
 *                               media_query
 *         ___________________________|_______________________________
 *        |                                                           |
 * {@literal @media} all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
 *                |______________________|
 *                          |
 *                    media_expression
 * </pre>
 */
 

/**
 * root method for the preprocessor.
 */
String pp_root(MapCSSStyleSource sheet):
{
}
{
    { sb = new StringBuilder(); this.sheet = sheet; }
    pp_black_box(true) <EOF>
    { return sb.toString(); }
}

/**
 * Parse any unknown grammar (black box).
 *
 * Only stop when "@media" is encountered and keep track of correct number of
 * opening and closing curly brackets.
 *
 * @param write false if this content should be skipped (@pp_media condition is not fulfilled), true otherwise
 */
void pp_black_box(boolean write):
{
    Token t;
}
{
    (
        (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); }
        |
            pp_w1()
        |
            pp_supports(!write)
        |
            pp_media(!write)
        |
            t=<LBRACE> { if (write) sb.append(t.image); } pp_black_box(write) t=<RBRACE> { if (write) sb.append(t.image); }
    )*
}

/**
 * Parses an @supports rule.
 *
 * @param ignore if the content of this rule should be ignored
 * (because we are already inside a @supports block that didn't pass)
 */
void pp_supports(boolean ignore):
{
    boolean pass;
}
{
    <PP_SUPPORTS> pp_w()
    pass=pp_supports_condition()
    <LBRACE>
    pp_black_box(pass && !ignore)
    <RBRACE>
}

/**
 * Parses the condition of the @supports rule.
 *
 * Unlike other parsing rules, grabs trailing whitespace.
 * @return true, if the condition is fulfilled
 */
boolean pp_supports_condition():
{
    boolean pass;
    boolean q;
}
{
    (
        <PP_NOT> pp_w() q=pp_supports_condition_in_parens() { pass = !q; } pp_w()
    |
        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_AND>)
        pass=pp_supports_condition_in_parens() pp_w()
        ( <PP_AND> pp_w() q=pp_supports_condition_in_parens() { pass = pass && q; } pp_w() )+
    |
        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_OR>)
        pass=pp_supports_condition_in_parens() pp_w()
        ( <PP_OR> pp_w() q=pp_supports_condition_in_parens() { pass = pass || q; } pp_w() )+
    |
        pass=pp_supports_condition_in_parens() pp_w()
    )
    { return pass; }
}

/**
 * Parses something in parenthesis inside the condition of the @supports rule.
 *
 * @return true, if the condition is fulfilled
 */
boolean pp_supports_condition_in_parens():
{
    boolean pass;
}
{
    (
        LOOKAHEAD(pp_supports_declaration_condition())
        pass=pp_supports_declaration_condition()
    |
        <LPAR> pp_w() pass=pp_supports_condition() <RPAR>
    )
    { return pass; }
}

/**
 * Parse an @supports declaration condition, e.&nbsp;g. a single (key:value) or (key) statement.
 *
 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
 *
 * @return true if the condition is fulfilled
 */
boolean pp_supports_declaration_condition():
{
    Token t;
    String feature;
    Object val = null;
}
{
    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
    { return this.sheet.evalSupportsDeclCondition(feature, val); }
}

// deprecated
void pp_media(boolean ignore):
{
    boolean pass = false;
    boolean q;
    boolean empty = true;
}
{
    <PP_MEDIA> pp_w()
    ( q=pp_media_query() { pass = pass || q; empty = false; }
        ( <COMMA> pp_w() q=pp_media_query() { pass = pass || q; } )*
    )?
    <LBRACE>
    pp_black_box((empty || pass) && !ignore)
    <RBRACE>
}

// deprecated
boolean pp_media_query():
{
    Token t;
    String mediatype = "all";
    boolean pass = true;
    boolean invert = false;
    boolean e;
}
{
    ( <PP_NOT> { invert = true; } pp_w() )?
    (
            t=<IDENT> { mediatype = t.image.toLowerCase(Locale.ENGLISH); } pp_w()
            ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
        |
            e=pp_media_expression() { pass = pass && e; } pp_w()
            ( <PP_AND> pp_w() e=pp_media_expression() { pass = pass && e; } pp_w() )*
    )
    { 
        if (!"all".equals(mediatype)) {
            pass = false;
        }
        return invert ? (!pass) : pass;
    }
}

/**
 * Parse an @media expression.
 *
 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
 *
 * @return true if the condition is fulfilled
 */
// deprecated
boolean pp_media_expression():
{
    Token t;
    String feature;
    Object val = null;
}
{
    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
    { return this.sheet.evalSupportsDeclCondition(feature, val); }
}

void pp_w1():
{
    Token t;
}
{
    t=<PP_NEWLINECHAR> { sb.append(t.image); }
        | 
    t=<PP_WHITESPACE> { sb.append(t.image); }
        |
    t=<PP_COMMENT_START> { sb.append(t.image); } t=<PP_COMMENT_END> { sb.append(t.image); }
}

void pp_w():
{
}
{
 ( pp_w1() )*
}

/*************
 *
 * Parser definition for the main MapCSS parser:
 *
 * <pre>
 *
 *                       rule
 *  _______________________|______________________________
 * |                                                      |
 *        selector                      declaration
 *  _________|___________________   _________|____________
 * |                             | |                      |
 *
 * way|z11-12[highway=residential] { color: red; width: 3 }
 *
 *    |_____||___________________|   |_________|
 *       |            |                   |
 *     zoom       condition          instruction
 *
 * more general:
 *
 * way|z13-[a=b][c=d]::subpart, way|z-3[u=v]:closed::subpart2 { p1 : val; p2 : val; }
 *
 * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
 *
 * </pre>
 */

int uint() :
{
    Token i;
}
{
    i=<UINT> { return Integer.parseInt(i.image); }
}

int int_() :
{
    int i;
}
{
    <MINUS> i=uint() { return -i; } | i=uint() { return i; }
}

float ufloat() :
{
    Token f;
}
{
    ( f=<UFLOAT> | f=<UINT> )
    { return Float.parseFloat(f.image); }
}

float float_() :
{
    float f;
}
{
    <MINUS> f=ufloat() { return -f; } | f=ufloat() { return f; }
}

String string() :
{
    Token t;
}
{
    t=<STRING>
    { return t.image.substring(1, t.image.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); }
}

String ident():
{
    Token t;
    String s;
}
{
    ( t=<IDENT> | t=<SET> ) { return t.image; }
}

String string_or_ident() :
{
    Token t;
    String s;
}
{
    ( s=ident() | s=string() ) { return s; }
}

String regex() :
{
    Token t;
}
{
    t=<REGEX>
    { return t.image.substring(1, t.image.length() - 1); }
}

/**
 * white-space
 */
void s() :
{
}
{
    ( <S> )?
}

/**
 * mix of white-space and comments
 */
void w() :
{
}
{
    ( <S> | <COMMENT_START> <COMMENT_END> )*
}

/**
 * comma delimited list of floats (at least 2, all &gt;= 0)
 */
List<Float> float_array() :
{
    float f;
    List<Float> fs = new ArrayList<Float>();
}
{
    f=ufloat() { fs.add(f); }
    (
        <COMMA> s()
        f=ufloat() { fs.add(f); }
    )+
    {
        return fs;
    }
}

/**
 * entry point for the main parser
 */
void sheet(MapCSSStyleSource sheet):
{
}
{
    { this.sheet = sheet; }
    w()
    (
        try {
            rule() w()
        } catch (MapCSSException mex) {
            error_skipto(RBRACE, mex);
            w();
        } catch (ParseException ex) {
            error_skipto(RBRACE, null);
            w();
        }
    )*
    <EOF>
}

void rule():
{
    List<Selector> selectors = new ArrayList<Selector>();
    Selector sel;
    Declaration decl;
}
{
    sel=child_selector() { selectors.add(sel); }
    (
        <COMMA> w()
        sel=child_selector() { selectors.add(sel); }
    )*
    decl=declaration()
    { 
        for (Selector s : selectors) {
            sheet.rules.add(new MapCSSRule(s, decl));
        }
    }
}

Selector child_selector() :
{
    Selector.ChildOrParentSelectorType type = null;
    Condition c;
    List<Condition> conditions = new ArrayList<Condition>();
    Selector selLeft;
    LinkSelector selLink = null;
    Selector selRight = null;
}
{
    selLeft=selector() w()
    (
        (
            (
                (
                    <GREATER> { type = Selector.ChildOrParentSelectorType.CHILD; }
                |
                    <LESS> { type = Selector.ChildOrParentSelectorType.PARENT; }
                |
                    <PLUS> { type = Selector.ChildOrParentSelectorType.SIBLING; }
                )
                ( ( c=condition(Context.LINK) | c=class_or_pseudoclass(Context.LINK) ) { conditions.add(c); } )*
            |
                <ELEMENT_OF> { type = Selector.ChildOrParentSelectorType.ELEMENT_OF; }
            |
                <CROSSING> { type = Selector.ChildOrParentSelectorType.CROSSING; }
            )
            w()
        |
            { /* <GREATER> is optional for child selector */ type = Selector.ChildOrParentSelectorType.CHILD; }
        )
        { selLink = new LinkSelector(conditions); }
        selRight=selector() w()
    )?
    { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, type) : selLeft; }
}

Selector selector() :
{
    Token base;
    Condition c;
    Pair<Integer, Integer> r = null;
    List<Condition> conditions = new ArrayList<Condition>();
    Subpart sub = null;
}
{
    ( base=<IDENT> | base=<STAR> )
    ( r=zoom() )?
    ( ( c=condition(Context.PRIMITIVE) | c=class_or_pseudoclass(Context.PRIMITIVE) ) { conditions.add(c); } )*
    ( sub=subpart() )?
    { return new GeneralSelector(base.image, r, conditions, sub); }
}

Pair<Integer, Integer> zoom() :
{
    Integer min = 0;
    Integer max = Integer.MAX_VALUE;
}
{
    <PIPE_Z>
    (
            <MINUS> max=uint()
        |
        LOOKAHEAD(2)
            min=uint() <MINUS> ( max=uint() )?
        |
            min=uint() { max = min; }
    )
    { return new Pair<Integer, Integer>(min, max); }
}

Condition condition(Context context) :
{
    Condition c;
    Expression e;
}
{
    <LSQUARE> s()
    (
        LOOKAHEAD( simple_key_condition(context) s() <RSQUARE> )
            c=simple_key_condition(context) s() <RSQUARE> { return c; }
        |
        LOOKAHEAD( simple_key_value_condition(context) s() <RSQUARE> )
            c=simple_key_value_condition(context) s() <RSQUARE> { return c; }
        |
            e=expression() <RSQUARE> { return Condition.createExpressionCondition(e, context); }
    )
}

String tag_key() :
{
    String s, s2;
    Token t;
}
{
        s=string() { return s; }
    |
        s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
}

Condition simple_key_condition(Context context) :
{
    boolean not = false;
    Condition.KeyMatchType matchType = null;;
    String key;
}
{
    ( <EXCLAMATION> { not = true; } )?
    (
        { matchType = Condition.KeyMatchType.REGEX; } key = regex()
    |
        key = tag_key()
    )
    ( LOOKAHEAD(2) <QUESTION> <EXCLAMATION> { matchType = Condition.KeyMatchType.FALSE; } )?
    (              <QUESTION>               { matchType = Condition.KeyMatchType.TRUE;  } )?
    { return Condition.createKeyCondition(key, not, matchType, context); }
}

Condition simple_key_value_condition(Context context) :
{
    String key;
    String val;
    float f;
    int i;
    Condition.Op op;
    boolean considerValAsKey = false;
}
{
    key=tag_key() s()
    (
        LOOKAHEAD(3)
            (
                    <EQUAL> <TILDE> { op=Condition.Op.REGEX; }
                |
                    <EXCLAMATION> <TILDE> { op=Condition.Op.NREGEX; }
            )
            s()
            ( <STAR> { considerValAsKey=true; } )?
            val=regex()
        |
            (
                    <EXCLAMATION> <EQUAL> { op=Condition.Op.NEQ; }
                |
                    <EQUAL> { op=Condition.Op.EQ; }
                |
                    <TILDE> <EQUAL> { op=Condition.Op.ONE_OF; }
                |
                    <CARET> <EQUAL> { op=Condition.Op.BEGINS_WITH; }
                |
                    <DOLLAR> <EQUAL> { op=Condition.Op.ENDS_WITH; }
                |
                    <STAR> <EQUAL> { op=Condition.Op.CONTAINS; }
            )
            s()
            ( <STAR> { considerValAsKey=true; } )?
            ( 
                LOOKAHEAD(2) 
                        i=int_() { val=Integer.toString(i); }
                    | 
                        f=float_() { val=Float.toString(f); } 
                    |
                        val=string_or_ident() 
            )
        |
            (
                    <GREATER_EQUAL> { op=Condition.Op.GREATER_OR_EQUAL; }
                |
                    <GREATER> { op=Condition.Op.GREATER; }
                |
                    <LESS_EQUAL> { op=Condition.Op.LESS_OR_EQUAL; }
                |
                    <LESS> { op=Condition.Op.LESS; }
            )
            s()
            f=float_() { val=Float.toString(f); }
    )
    { return Condition.createKeyValueCondition(key, val, op, context, considerValAsKey); }
}

Condition class_or_pseudoclass(Context context) :
{
    String s;
    boolean not = false;
    boolean pseudo;
}
{
    ( <EXCLAMATION> { not = true; } )? 
    (
        <FULLSTOP> { pseudo = false; }
    |
        <COLON> { pseudo = true; }
    )
    s=ident()
    { return pseudo
        ? Condition.createPseudoClassCondition(s, not, context)
        : Condition.createClassCondition(s, not, context); }
}

Subpart subpart() :
{
    String s;
    Expression e;
}
{
    <DCOLON>
    (
        s=ident() { return new Subpart.StringSubpart(s); }
    |
        <STAR> { return new Subpart.StringSubpart("*"); }
    |
        <LPAR> e=expression() <RPAR> { return new Subpart.ExpressionSubpart(e); }
    )
}

Declaration declaration() :
{
    List<Instruction> ins = new ArrayList<Instruction>();
    Instruction i;
    Token key;
    Object val = null;
}
{
    <LBRACE> w()
    (
        (
            <SET> w()
            (<FULLSTOP>)? // specification allows "set .class" to set "class". we also support "set class"
            key=<IDENT> w()
            ( <EQUAL> val=expression() )?
            { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
            ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
        )
    |
        key=<IDENT> w() <COLON> w()
        (
            LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
                val=float_array()
                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
                w()
                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
            |
            LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
                val=expression()
                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
            |
                val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
        )
    )*
    <RBRACE>
    { return new Declaration(ins, declarationCounter++); }
}

/**
 * General expression.
 * Separate production rule for each level of operator precedence (recursive descent).
 */
Expression expression() :
{
    Expression e;
}
{
    e=conditional_expression()
    {
        return e;
    }
}

Expression conditional_expression() :
{
    Expression e, e1, e2;
    String op = null;
}
{
    e=or_expression()
    (
        <QUESTION> w()
        e1=conditional_expression()
        <COLON> w()
        e2=conditional_expression()
        {
            e = ExpressionFactory.createFunctionExpression("cond", Arrays.asList(e, e1, e2));
        }
    )?
    {
        return e;
    }
}

Expression or_expression() :
{
    Expression e, e2;
    String op = null;
}
{
    e=and_expression()
    (
        <PIPE> <PIPE> w()
        e2=and_expression()
        {
            e = ExpressionFactory.createFunctionExpression("or", Arrays.asList(e, e2));
        }
    )*
    {
        return e;
    }
}

Expression and_expression() :
{
    Expression e, e2;
    String op = null;
}
{
    e=relational_expression()
    (
        <AMPERSAND> <AMPERSAND> w()
        e2=relational_expression()
        {
            e = ExpressionFactory.createFunctionExpression("and", Arrays.asList(e, e2));
        }
    )*
    {
        return e;
    }
}

Expression relational_expression() :
{
    Expression e, e2;
    String op = null;
}
{
    e=additive_expression()
    (
        (
            <GREATER_EQUAL> { op = "greater_equal"; }
            |
            <LESS_EQUAL> { op = "less_equal"; }
            |
            <GREATER> { op = "greater"; }
            |
            <LESS> { op = "less"; }
            |
            <EQUAL> ( <EQUAL> )? { op = "equal"; }
            |
            <EXCLAMATION> <EQUAL> { op = "not_equal"; }
        ) w()
        e2=additive_expression()
        {
            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
        }
    )?
    {
        return e;
    }
}

Expression additive_expression() :
{
    Expression e, e2;
    String op = null;
}
{
    e=multiplicative_expression()
    (
        ( <PLUS> { op = "plus"; } | <MINUS> { op = "minus"; } ) w()
        e2=multiplicative_expression()
        {
            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
        }
    )*
    {
        return e;
    }
}

Expression multiplicative_expression() :
{
    Expression e, e2;
    String op = null;
}
{
    e=unary_expression()
    (
        ( <STAR> { op = "times"; } | <SLASH> { op = "divided_by"; } ) w()
        e2=unary_expression()
        {
            e = ExpressionFactory.createFunctionExpression(op, Arrays.asList(e, e2));
        }
    )*
    {
        return e;
    }
}

Expression unary_expression() :
{
    Expression e;
    String op = null;
}
{
    (
        <MINUS> { op = "minus"; } w()
    |
        <EXCLAMATION> { op = "not"; } w()
    )?
    e=primary() w()
    {
        if (op == null)
            return e;
        return ExpressionFactory.createFunctionExpression(op, Collections.singletonList(e));
    }
}

Expression primary() :
{
    Expression nested;
    Expression fn;
    Object lit;
}
{
    LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
        fn=function() { return fn; }
    |
        lit=literal()
        { 
            if (lit == null)
                return NullExpression.INSTANCE;
            return new LiteralExpression(lit);
        }
    |
        <LPAR> w() nested=expression() <RPAR> { return nested; }
}

Expression function() :
{
    Expression arg;
    String name;
    List<Expression> args = new ArrayList<Expression>();
}
{
    name=ident() w()
    <LPAR> w()
    (
        arg=expression() { args.add(arg); }
        ( <COMMA> w() arg=expression() { args.add(arg); } )*
    )?
    <RPAR>
    { return ExpressionFactory.createFunctionExpression(name, args); }
}

Object literal() :
{
    String val, pref;
    Token t;
    Float f;
}
{
        LOOKAHEAD(2)
        pref=ident() t=<HEXCOLOR>
        { return Main.pref.getColor("mappaint." + (sheet == null ? "MapCSS" : sheet.title) + "." + pref, ColorHelper.html2color(t.image)); }
    |
        t=<IDENT> { return new Keyword(t.image); }
    |
        val=string() { return val; }
    |
        <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
    |
        LOOKAHEAD(2)
        f=ufloat_unit() { return f; }
    |
        f=ufloat() { return f; }
    |
        t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
}

/**
 * Number followed by a unit.
 *
 * Returns angles in radians and lengths in pixels.
 */
Float ufloat_unit() :
{
    float f;
    String u;
}
{
    f=ufloat() ( u=ident() | <DEG> { u = "°"; } )
    {
        Double m = unit_factor(u);
        if (m == null)
            return null;
        return (float) (f * m);
    }
}

JAVACODE
private Double unit_factor(String unit) {
    switch (unit) {
        case "deg":
        case "°": return Math.PI / 180;
        case "rad": return 1.;
        case "grad": return Math.PI / 200;
        case "turn": return 2 * Math.PI;
        case "px": return 1.;
        case "cm": return 96/2.54;
        case "mm": return 9.6/2.54;
        case "in": return 96.;
        case "q": return 2.4/2.54;
        case "pc": return 16.;
        case "pt": return 96./72;
        default: return null;
    }
}

JAVACODE
void error_skipto(int kind, MapCSSException me) {
    if (token.kind == EOF)
        throw new ParseException("Reached end of file while parsing");
        
    Exception e = null;        
    ParseException pe = generateParseException();

    if (me != null) {
        me.setLine(pe.currentToken.next.beginLine);
        me.setColumn(pe.currentToken.next.beginColumn);
        e = me;
    } else {
        e = new ParseException(pe.getMessage()); // prevent memory leak
    }
    
    Main.error("Skipping to the next rule, because of an error:");
    Main.error(e);
    if (sheet != null) {
        sheet.logError(e);
    }
    Token t;
    do {
        t = getNextToken();
    } while (t.kind != kind && t.kind != EOF);
    if (t.kind == EOF)
        throw new ParseException("Reached end of file while parsing");
}

JAVACODE
/**
 * read everything to the next semicolon
 */
String readRaw() {
    Token t;
    StringBuilder s = new StringBuilder();
    while (true) {
        t = getNextToken();
        if ((t.kind == S || t.kind == STRING || t.kind == UNEXPECTED_CHAR) &&
                t.image.contains("\n")) {
            ParseException e = new ParseException(String.format("Warning: end of line while reading an unquoted string at line %s column %s.", t.beginLine, t.beginColumn));
            Main.error(e);
            if (sheet != null) {
                sheet.logError(e);
            }
        }
        if (t.kind == SEMICOLON || t.kind == EOF)
            break;
        s.append(t.image);
    }
    if (t.kind == EOF)
        throw new ParseException("Reached end of file while parsing");
    return s.toString();
}

