Index: trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj	(revision 4252)
+++ trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj	(revision 4252)
@@ -0,0 +1,618 @@
+// License: GPL. For details, see LICENSE file.
+options {
+  STATIC = false;
+}
+
+PARSER_BEGIN(MapCSSParser)
+package org.openstreetmap.josm.gui.mappaint.mapcss.parsergen;
+
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.List;
+
+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.Instruction;
+import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
+import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
+import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
+import org.openstreetmap.josm.gui.mappaint.mapcss.Expression.FunctionExpression;
+import org.openstreetmap.josm.gui.mappaint.mapcss.Expression.LiteralExpression;
+import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSException;
+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.tools.Pair;
+
+public class MapCSSParser {
+    MapCSSStyleSource sheet;
+}
+PARSER_END(MapCSSParser)
+
+/*************
+ * Token definitions
+ */
+
+<DEFAULT>
+TOKEN:
+{
+    < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
+|   < UINT: ["1"-"9"] ( ["0"-"9"] )* >
+|   < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
+|   < STRING: "\"" ( [" ","!","#"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\\"" | "\\\\" )*  "\"" >
+|   < #REGEX_CHAR_WITHOUT_STAR: [" "-")","+"-".","0"-"[","]"-"~","\u0080"-"\uFFFF"] | "\\/" | "\\\\" >
+|   < REGEX: "/" <REGEX_CHAR_WITHOUT_STAR> ( <REGEX_CHAR_WITHOUT_STAR> | "*" )*  "/" >
+|   < #H: ["0"-"9","a"-"f","A"-"F"] >
+|   < HEXCOLOR: "#" ( <H><H><H><H><H><H> | <H><H><H> ) >
+|   < S: ( " " | "\t" | "\n" | "\r" | "\f" )+ >
+|   < STAR: "*" >
+|   < SLASH: "/" >
+|   < LBRACE: "{" >
+|   < RBRACE: "}" >
+|   < LSQUARE: "[" >
+|   < RSQUARE: "]" >
+|   < LPAR: "(" >
+|   < RPAR: ")" >
+|   < GREATER_EQUAL: ">=" >
+|   < LESS_EQUAL: "<=" >
+|   < GREATER: ">" >
+|   < LESS: "<" >
+|   < EQUAL: "=" >
+|   < EXCLAMATION: "!" >
+|   < TILDE: "~" >
+|   < COLON: ":" >
+|   < DCOLON: "::" >
+|   < SEMICOLON: ";" >
+|   < COMMA: "," >
+|   < PIPE: "|" >
+|   < PIPE_Z: "|z" >
+|   < PLUS: "+" >
+|   < MINUS: "-" >
+|   < AMPERSAND: "&" >
+|   < QUESTION: "?" >
+|   < DOLLAR: "$" >
+|   < CARET: "^" >
+|   < COMMENT_START: "/*" > : COMMENT
+|   < UNEXPECTED_CHAR : ~[] > // avoid TokenMgrErrors because they are hard to recover from
+}
+
+<COMMENT>
+TOKEN:
+{
+    < COMMENT_END: "*/" > : DEFAULT
+}
+
+<COMMENT>
+SKIP:
+{
+    < ~[] >
+}
+
+/*************
+ * Parser definitions
+ *
+ *                       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".
+ *
+ */
+
+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 string_or_ident() :
+{
+    Token t;
+    String s;
+}
+{
+    t=<IDENT> { return t.image; } | 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 >= 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;
+    }
+}
+
+/**
+ * root
+ */
+void sheet(MapCSSStyleSource sheet):
+{
+    MapCSSRule r;
+    Token com = null;
+}
+{
+    { this.sheet = sheet; }
+    w()
+    (
+        try {
+                r=rule() { if (r != null) { sheet.rules.add(r); } } w()
+        } catch (MapCSSException mex) {
+            error_skipto(RBRACE, mex);
+            w();
+        } catch (ParseException ex) {
+            error_skipto(RBRACE, null);
+            w();
+        }
+    )*
+    <EOF>
+}
+
+MapCSSRule rule():
+{
+    List<Selector> selectors = new ArrayList<Selector>();
+    Selector sel;
+    List<Instruction> decl;
+}
+{
+    sel=child_selector() { selectors.add(sel); }
+    (
+        <COMMA> w()
+        sel=child_selector() { selectors.add(sel); }
+    )*
+    decl=declaration()
+    { return new MapCSSRule(selectors, decl); }
+}
+
+Selector child_selector() :
+{
+    boolean parentSelector = false;
+    Condition c;
+    List<Condition> conditions = new ArrayList<Condition>();
+    Selector selLeft;
+    LinkSelector selLink = null;
+    Selector selRight = null;
+}
+{
+    selLeft=selector() w()
+    (
+        ( <GREATER> { parentSelector = false; } | <LESS> { parentSelector = true; } ) 
+        ( ( c=condition(Context.LINK) | c=pseudoclass(Context.LINK) ) { conditions.add(c); } )*
+        { selLink = new LinkSelector(conditions); }
+        w()
+        selRight=selector() w()
+    )?
+    { return selRight != null ? new ChildOrParentSelector(selLeft, selLink, selRight, parentSelector) : selLeft; }
+}
+
+Selector selector() :
+{
+    Token base;
+    Condition c;
+    Pair<Integer, Integer> r = null;
+    List<Condition> conditions = new ArrayList<Condition>();
+    String sub = null;
+}
+{
+    ( base=<IDENT> | base=<STAR> )
+    ( r=zoom() )?
+    ( ( c=condition(Context.PRIMITIVE) | c=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()
+        |
+            min=uint() ( <MINUS> ( max=uint() )? )?
+    )
+    { 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.create(e, context); }
+    )
+}
+
+String tag_key() :
+{
+    String s;
+    Token t;
+}
+{
+        s=string() { return s; }
+    |
+        t=<IDENT> { s = t.image; } ( <COLON> t=<IDENT> { s += ':' + t.image; } )* { return s; }
+}
+
+Condition simple_key_condition(Context context) :
+{
+    boolean not = false;
+    boolean yes = false;
+    String key;
+}
+{
+    ( <EXCLAMATION> { not = true; } )?
+    key=tag_key()
+    ( <QUESTION> { yes = true; } )?
+    { return Condition.create(key, not, yes, context); }
+}
+
+Condition simple_key_value_condition(Context context) :
+{
+    String key;
+    String val;
+    float f;
+    int i;
+    Condition.Op op;
+}
+{
+    key=tag_key() s()
+    (
+        LOOKAHEAD(2)
+            <EQUAL> <TILDE> { op=Condition.Op.REGEX; } s() 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()
+            ( 
+                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.create(key, val, op, context); }
+}
+
+Condition pseudoclass(Context context) :
+{
+    Token t;
+    boolean not = false;
+}
+{
+    ( <EXCLAMATION> { not = true; } )? 
+    <COLON>
+    t=<IDENT>
+    { return Condition.create(t.image, not, context); }
+}
+
+String subpart() :
+{
+    Token t;
+}
+{
+    <DCOLON>
+    ( t=<IDENT> | t=<STAR> )
+    { return t.image; }
+}
+
+List<Instruction> declaration() :
+{
+    List<Instruction> ins = new ArrayList<Instruction>();
+    Instruction i;
+    Token key;
+    Object val;
+}
+{
+    <LBRACE> w()
+    (
+        key=<IDENT> w() <COLON> w()
+        (
+            LOOKAHEAD( float_array() w() ( <SEMICOLON> | <RBRACE> ) )
+                val=float_array()
+                { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
+                w()
+                ( <RBRACE> { return ins; } | <SEMICOLON> w() )
+            |
+            LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
+                val=expression()
+                { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
+                ( <RBRACE> { return ins; } | <SEMICOLON> w() )
+            |
+                val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val)); }
+        )
+    )*
+    <RBRACE>
+    { return ins; }
+}
+
+Expression expression():
+{
+    List<Expression> args = new ArrayList<Expression>();
+    Expression e;
+    String op = null;
+}
+{
+    (
+        <EXCLAMATION> { op = "not"; } w() e=primary() { args.add(e); } w()
+    |
+        <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w()
+    |
+
+        (
+            e=primary() { args.add(e); } w()
+            (
+                    ( <PLUS> { op = "plus"; } w() e=primary() { args.add(e); } w() )+
+                |
+                    ( <STAR> { op = "times"; } w() e=primary() { args.add(e); } w() )+
+                |
+                    ( <MINUS> { op = "minus"; } w() e=primary() { args.add(e); } w() )+
+                |
+                    ( <SLASH> { op = "divided_by"; } w() e=primary() { args.add(e); } w() )+
+                |
+                    <GREATER_EQUAL> { op = "greater_equal"; } w() e=primary() { args.add(e); } w()
+                |
+                    <LESS_EQUAL> { op = "less_equal"; } w() e=primary() { args.add(e); } w()
+                |
+                    <GREATER> { op = "greater"; } w() e=primary() { args.add(e); } w()
+                |
+                    <EQUAL> ( <EQUAL> )? { op = "equal"; } w() e=primary() { args.add(e); } w()
+                |
+                    <LESS> { op = "less"; } w() e=primary() { args.add(e); } w()
+                |
+                    <AMPERSAND> <AMPERSAND> { op = "and"; } w() e=primary() { args.add(e); } w()
+                |
+                    <PIPE> <PIPE> { op = "or"; } w() e=primary() { args.add(e); } w()
+                |
+                    <QUESTION> { op = "cond"; } w() e=primary() { args.add(e); } w() <COLON> w() e=primary() { args.add(e); } w()
+            )?
+        )
+    )
+    {
+        if (op == null)
+            return args.get(0);
+        return new FunctionExpression(op, args);
+    }
+}
+
+Expression primary() :
+{
+    Expression nested;
+    FunctionExpression fn;
+    Object lit;
+}
+{
+    LOOKAHEAD(3) // both function and identifier start with an identifier (+ optional whitespace)
+        fn=function() { return fn; }
+    |
+        lit=literal() { return new LiteralExpression(lit); }
+    |
+        <LPAR> w() nested=expression() <RPAR> { return nested; }
+}
+
+FunctionExpression function() :
+{
+    Token tmp;
+    Expression arg;
+    String name;
+    List<Expression> args = new ArrayList<Expression>();
+}
+{
+    tmp=<IDENT> { name = tmp.image; } w()
+    <LPAR> w()
+    (
+        arg=expression() { args.add(arg); }
+        ( <COMMA> w() arg=expression() { args.add(arg); } )*
+    )?
+    <RPAR>
+    { return new FunctionExpression(name, args); }
+}
+
+Object literal() :
+{
+    String val;
+    Token t;
+    float f;
+}
+{
+        t=<IDENT> { return new Keyword(t.image); }
+    |
+        val=string() { return val; }
+    |
+        <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
+    |
+        f=ufloat() { return f; }
+    |
+        t=<HEXCOLOR>
+            {
+                String clr = t.image.substring(1);
+                if (clr.length() == 3) {
+                    clr = new String(new char[] {clr.charAt(0),clr.charAt(0),clr.charAt(1),clr.charAt(1),clr.charAt(2),clr.charAt(2)});
+                }
+                if (clr.length() != 6)
+                    throw new AssertionError();
+                return new Color(Integer.parseInt(clr, 16));
+            }
+}
+
+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
+    }
+    
+    System.err.println("Skipping to the next rule, because of an error:");
+    System.err.println(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));
+            System.err.println(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();
+}
+
Index: trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java	(revision 4251)
+++ trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java	(revision 4252)
@@ -20,7 +20,7 @@
 import org.openstreetmap.josm.gui.mappaint.StyleSource;
 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
-import org.openstreetmap.josm.gui.mappaint.mapcss.parser.MapCSSParser;
-import org.openstreetmap.josm.gui.mappaint.mapcss.parser.ParseException;
-import org.openstreetmap.josm.gui.mappaint.mapcss.parser.TokenMgrError;
+import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
+import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
+import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.TokenMgrError;
 import org.openstreetmap.josm.gui.preferences.SourceEntry;
 import org.openstreetmap.josm.io.MirroredInputStream;
