All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.monoid.web.jp.javacc.json.jj Maven / Gradle / Ivy

The newest version!
/*@bgen(jjtree) Generated By:JJTree: Do not edit this line. json.jj */
/*@egen*/options
{
  STATIC = false;
  DEBUG_PARSER = false;
  ERROR_REPORTING = true;
                           
                                    
                              
}

PARSER_BEGIN(JSONPathCompiler)
package us.monoid.web.jp.javacc;

import us.monoid.json.*;
import java.util.*;

public class JSONPathCompiler/*@bgen(jjtree)*/implements JSONPathCompilerTreeConstants/*@egen*/ {/*@bgen(jjtree)*/
  protected JJTJSONPathCompilerState jjtree = new JJTJSONPathCompilerState();

/*@egen*/
  
  public static class JSONPathExpr extends AbstractJSONExpr {
    JSONPathExpr(int i) {
      super (i);
    }
    public void dump(String prefix) {
      System.out.println(prefix + toString());
      if (children != null && children.length > 0) {
        System.out.println(prefix + " [");
        for (int i = 0; children != null && i < children.length; ++i) {
          System.out.print(prefix + " (" + (i + 1) + ")");
          SimpleNode c = (SimpleNode) children [i];
          if (c == null) {
            System.out.println("null");
          } else {
            c.dump(prefix + " ");
          }
        }
        System.out.println(prefix + " ]");
      }
    }

    public Object eval(Object o) throws JSONException {
      if (o == null) return null;
      switch (this.id) {
        case JJTIMAGE:
        {
			return this.value;
        }
        case JJTEXPRESSION: {
          if (getChildrenCount() == 0) return null;
          for (int i = 0; o != null && i < getChildrenCount(); ++i) {
            o = at(i).eval(o);
          }
          return o;
        }
        case JJTARRAY: {
          if (this.value instanceof Integer) {
          int index = Integer.class.cast(this.value);
          if (o instanceof JSONArray) {
            JSONArray array = JSONArray.class.cast(o);
            return array.get(index);
     	   } else {
    	    return null;
    	   }
    	 } else { // AST contains a selector expression. evaluate it against all objects in array
 			if (o instanceof JSONArray) {
            	JSONArray array = JSONArray.class.cast(o);
            	// match first strategy. only one item is returned
            	Object result = null;
            	match:
            	for (int i = 0, len = array.length(); i < len; i++) {
            	  Object item = array.get(i);
            	  boolean test = at(0).test(item);
           		  if (test) {
           		    result = item; // evaluation can continue on this item
           		  	break;
           		 }
            	}
            	return result;
        	}  	
    	 }
        }
        case JJTOBJECT: {
          if (o instanceof JSONObject) {
            JSONObject json = JSONObject.class.cast(o);
            return json.get(this.value.toString());
          } else if (o instanceof JSONArray)
          {
            JSONArray array = JSONArray.class.cast(o);
           // return all items
           List matches = new ArrayList();
            Object result = null;
            for (int i = 0, len = array.length(); i < len; i++) {
              Object item = array.get(i);
              if (item instanceof JSONObject)
              {
			     JSONObject obj = (JSONObject)item;
			     if (obj.has(this.value.toString()))
			     {
			       matches.add(obj.get(this.value.toString()));
			     }
              }
           }
           return matches;
          }
          return null;
        }
        
        default : System.err.println("Doesn't handle " + this.id);
        break;
      }
      return null;
    }
  }

  static SimpleNode jjtCreate(int id)
  {
    return new JSONPathExpr(id);
  }

  public static void main(String args []) throws Exception
  {
    try
    {
      java.io.StringReader r = new java.io.StringReader(args [0]);
      JSONPathCompiler app = new JSONPathCompiler(r);
      JSONPathExpr x = JSONPathExpr.class.cast(app.expr());
      x.dump(" ");
      JSONObject json = new JSONObject("{ \"a\": [" +
      "{\"Hello\": \"World\"},{\"Hello\": \"JB\"}]" +
      ", \"b\": null, \"c\": { \"d\": 1234 }}");
      Object o = x.eval(json);
      System.err.println("Compiling " + args [0] + "");
      System.err.println("Eval " + json + "");
      System.err.println("Result is:" + o + "");
    } catch (Exception err) {
      err.printStackTrace();
    }
  }
}

PARSER_END(JSONPathCompiler)

SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
}

TOKEN :
{
  < OPEN_ARRAY : "[" >
| < CLOSE_ARRAY : "]" >
|   < #LETTER : [ "$", "_", "a"-"z", "A"-"Z" ] >
| < CNAME : "'" (~["'"])* "'" >
| < #DIGIT : [ "0"-"9" ] >
| < #DIGIT19 : [ "1"-"9" ] >
| < OTHER : "-" >
| < DOT : "." >
| < OR : "||" >
| < AND : "&&" >
| < NOT : "!" >
| < OP : [ ">", "<", "="] > 
| < INTEGER : ([ "-" ])? (< DIGIT > | < DIGIT19 > (< DIGIT >)* ) >
| < NUMBER : < INTEGER > < FRAC >  | < INTEGER >  | < INTEGER > < FRAC > >
| < FRAC: < DOT > (< DIGIT >)+ >
| < EXP: < EXPONENT > (< DIGIT >)+ >
| < EXPONENT: ["E", "e"] ([ "+", "-" ])? >
| < IDENTIFIER :
    < LETTER >
    (
      < LETTER >
    | < DIGIT >
    | < OTHER >
    )* >

}

JSONPathExpr json()             :
{/*@bgen(jjtree) EXPRESSION */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTEXPRESSION);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
}
{/*@bgen(jjtree) EXPRESSION */
  try {
/*@egen*/
  expr() < EOF >/*@bgen(jjtree)*/
   {
     jjtree.closeNodeScope(jjtn000, true);
     jjtc000 = false;
   }
/*@egen*/
   {
    return jjtn000;
  }/*@bgen(jjtree)*/
  } catch (Throwable jjte000) {
    if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    if (jjte000 instanceof RuntimeException) {
      throw (RuntimeException)jjte000;
    }
    if (jjte000 instanceof ParseException) {
      throw (ParseException)jjte000;
    }
    throw (Error)jjte000;
  } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
  }
/*@egen*/
}

JSONPathExpr expr()             :
{/*@bgen(jjtree) EXPRESSION */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTEXPRESSION);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) EXPRESSION */
  try {
/*@egen*/
  (
    array()
  | object()
  )
  (
    array()
  | < DOT > object()
  )*/*@bgen(jjtree)*/
  {
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
  }
/*@egen*/
  
  {
    return jjtn000;
  }/*@bgen(jjtree)*/
  } catch (Throwable jjte000) {
    if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    if (jjte000 instanceof RuntimeException) {
      throw (RuntimeException)jjte000;
    }
    if (jjte000 instanceof ParseException) {
      throw (ParseException)jjte000;
    }
    throw (Error)jjte000;
  } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
  }
/*@egen*/
}

void array()        :
{/*@bgen(jjtree) ARRAY */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTARRAY);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
  Token i;
}
{/*@bgen(jjtree) ARRAY */
    try {
/*@egen*/
    < OPEN_ARRAY > (i = < INTEGER > 
  {
    jjtn000.value = new Integer(i.image);
  }
  | selector()
  {
    
  }
  ) < CLOSE_ARRAY >/*@bgen(jjtree)*/
    } catch (Throwable jjte000) {
      if (jjtc000) {
        jjtree.clearNodeScope(jjtn000);
        jjtc000 = false;
      } else {
        jjtree.popNode();
      }
      if (jjte000 instanceof RuntimeException) {
        throw (RuntimeException)jjte000;
      }
      if (jjte000 instanceof ParseException) {
        throw (ParseException)jjte000;
      }
      throw (Error)jjte000;
    } finally {
      if (jjtc000) {
        jjtree.closeNodeScope(jjtn000, true);
      }
    }
/*@egen*/ 
}


void selector() :
{/*@bgen(jjtree) selector */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTSELECTOR);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
}
{/*@bgen(jjtree) selector */
        try {
/*@egen*/
	term() ( < OR > term() )*/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
          }
        }
/*@egen*/

}

void term() :
{/*@bgen(jjtree) term */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTTERM);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
}
{/*@bgen(jjtree) term */
  try {
/*@egen*/
  neg() ( < AND > neg() )*/*@bgen(jjtree)*/
  } catch (Throwable jjte000) {
    if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    if (jjte000 instanceof RuntimeException) {
      throw (RuntimeException)jjte000;
    }
    if (jjte000 instanceof ParseException) {
      throw (ParseException)jjte000;
    }
    throw (Error)jjte000;
  } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
  }
/*@egen*/
   
}

void neg() :
{/*@bgen(jjtree) neg */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTNEG);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
  Token n = null;
}
{/*@bgen(jjtree) neg */
        try {
/*@egen*/
	[ n = < NOT > ] part()/*@bgen(jjtree)*/
        {
          jjtree.closeNodeScope(jjtn000, true);
          jjtc000 = false;
        }
/*@egen*/
	{
	  if (n != null) {
	    jjtn000.value = Boolean.FALSE;
	  }	    
	}/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
          }
        }
/*@egen*/
}

void part() :
{/*@bgen(jjtree) part */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTPART);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
}
{/*@bgen(jjtree) part */
        try {
/*@egen*/
	predicate() | "(" selector() ")"/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
          }
        }
/*@egen*/

}

void predicate():
{/*@bgen(jjtree) predicate */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTPREDICATE);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
  JSONPathExpr leftExpr;
  JSONPathExpr rightExpr = null;
  Token test = null;
}
{/*@bgen(jjtree) predicate */
        try {
/*@egen*/
	leftExpr = primaryExpr() ( test = < OP > rightExpr = primaryExpr())?/*@bgen(jjtree)*/
        {
          jjtree.closeNodeScope(jjtn000, true);
          jjtc000 = false;
        }
/*@egen*/
	{
	  	jjtn000.value = new Predicates.Operator(leftExpr, test.image, rightExpr);
	}/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
          }
        }
/*@egen*/	
}

JSONPathExpr primaryExpr()            :
{/*@bgen(jjtree) EXPRESSION */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTEXPRESSION);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
}
{/*@bgen(jjtree) EXPRESSION */
    try {
/*@egen*/
    (
      expr() |
    literalOrNumber()
  )/*@bgen(jjtree)*/
    {
      jjtree.closeNodeScope(jjtn000, true);
      jjtc000 = false;
    }
/*@egen*/
    {
    return jjtn000;
  }/*@bgen(jjtree)*/
    } catch (Throwable jjte000) {
      if (jjtc000) {
        jjtree.clearNodeScope(jjtn000);
        jjtc000 = false;
      } else {
        jjtree.popNode();
      }
      if (jjte000 instanceof RuntimeException) {
        throw (RuntimeException)jjte000;
      }
      if (jjte000 instanceof ParseException) {
        throw (ParseException)jjte000;
      }
      throw (Error)jjte000;
    } finally {
      if (jjtc000) {
        jjtree.closeNodeScope(jjtn000, true);
      }
    }
/*@egen*/
}

JSONPathExpr literalOrNumber()       :
{/*@bgen(jjtree) IMAGE */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTIMAGE);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
  Token string = null, number = null;
}
{/*@bgen(jjtree) IMAGE */
  try {
/*@egen*/
  (
    string = < CNAME > |
	number = <  NUMBER > |
	number = < INTEGER >
)/*@bgen(jjtree)*/
  {
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
  }
/*@egen*/
 
  {
    if (number != null)
    {
      jjtn000.value = new Double(number.image);
    } else if (string != null)
    {
      jjtn000.value = string.image.substring(1,string.image.length() - 1);
    }
    return jjtn000;
  }/*@bgen(jjtree)*/
  } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
  }
/*@egen*/
}

void object()         :
{/*@bgen(jjtree) OBJECT */
  JSONPathExpr jjtn000 = (JSONPathExpr)JSONPathCompiler.jjtCreate(JJTOBJECT);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
/*@egen*/
  Token name;
}
{/*@bgen(jjtree) OBJECT */
  try {
/*@egen*/
  (name = < IDENTIFIER >)/*@bgen(jjtree)*/
  {
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
  }
/*@egen*/
  {
    jjtn000.value = name.image;
  }/*@bgen(jjtree)*/
  } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
  }
/*@egen*/
}