String getQuotedString() throws ParseException { eatws(); char delim = peekChar(); if (!(delim=='\"' || delim=='\'')) { return null; } int val_start = ++pos; StringBuilder sb = new StringBuilder(); // needed for escaping for(;;) { if (pos>=end) { throw new ParseException("Missing end quote for string at pos " + (val_start-1) + " str='"+val+"'"); } char ch = val.charAt(pos); if (ch=='\\') { ch = pos<end ? val.charAt(pos++) : 0; } else if (ch==delim) { pos++; return sb.toString(); } sb.append(ch); pos++; } }
static int parseLocalParams(String txt, int start, Map<String,String> target, SolrParams params) throws ParseException { int off=start; if (!txt.startsWith(LOCALPARAM_START,off)) return start; StrParser p = new StrParser(txt,start,txt.length()); p.pos+=2; // skip over "{!" char ch = p.peek(); if (ch==LOCALPARAM_END) { return p.pos+1; String id = p.getId(); if (id.length()==0) { throw new ParseException("Expected identifier '}' parsing local params '" + txt + '"'); ch = p.peek(); if (ch!='=') { ch = p.peek(); if (ch=='\"' || ch=='\'') { val = p.getQuotedString(); } else if (ch=='$') { p.pos++; String pname = p.getId(); if (params!=null) { val = params.get(pname);
public String parseArg() throws ParseException { sp.eatws(); char ch = sp.peek(); String val = null; switch (ch) { case '$': sp.pos++; String param = sp.getId(); val = getParam(param); break; case '\'': case '"': val = sp.getQuotedString(); break; default: sp.eatws(); consumeArgumentDelimiter(); return val;
float getFloat() throws ParseException { eatws(); char[] arr = new char[end-pos]; int i; for (i=0; i<arr.length; i++) { char ch = val.charAt(pos); if ( (ch>='0' && ch<='9') || ch=='+' || ch=='-' || ch=='.' || ch=='e' || ch=='E' ) { pos++; arr[i]=ch; } else { break; } } return Float.parseFloat(new String(arr,0,i)); }
/** * Consume an argument delimiter (a comma) from the token stream. * Only consumes if more arguments should exist (no ending parens or end of string). * * @return whether a delimiter was consumed * @throws ParseException */ protected boolean consumeArgumentDelimiter() throws ParseException { /* if a list of args is ending, don't expect the comma */ if (hasMoreArguments()) { sp.expect(","); return true; } return false; }
String getId() throws ParseException { eatws(); int id_start=pos; if (pos<end && Character.isJavaIdentifierStart(val.charAt(pos))) { pos++; while (pos<end) { char ch = val.charAt(pos); if (!Character.isJavaIdentifierPart(ch) && ch!='.') { break; } pos++; } return val.substring(id_start, pos); } throw new ParseException("Expected identifier at pos " + pos + " str='" + val + "'"); }
public Query parse() throws ParseException { sp = new QueryParsing.StrParser(getString()); ValueSource vs = parseValueSource(); /*** boost promoted to top-level query type to avoid this hack // HACK - if this is a boosted query wrapped in a value-source, return // that boosted query instead of a FunctionQuery if (vs instanceof QueryValueSource) { Query q = ((QueryValueSource)vs).getQuery(); if (q instanceof BoostedQuery) return q; } ***/ return new FunctionQuery(vs); }
char peek() { eatws(); return pos<end ? val.charAt(pos) : 0; }
/** * TODO: Doc * * @throws ParseException */ public String parseId() throws ParseException { String value = sp.getId(); consumeArgumentDelimiter(); return value; }
/** * Parse a float. * * @return Float * @throws ParseException */ public Float parseFloat() throws ParseException { float value = sp.getFloat(); consumeArgumentDelimiter(); return value; }
void expect(String s) throws ParseException { eatws(); int slen=s.length(); if (val.regionMatches(pos, s, 0, slen)) { pos+=slen; } else { throw new ParseException("Expected '"+s+"' at position " + pos + " in '"+val+"'"); } }
boolean opt(String s) { eatws(); int slen=s.length(); if (val.regionMatches(pos, s, 0, slen)) { pos+=slen; return true; } return false; }
/** * Are there more arguments in the argument list being parsed? * * @return whether more args exist * @throws ParseException */ public boolean hasMoreArguments() throws ParseException { int ch = sp.peek(); /* determine whether the function is ending with a paren or end of str */ return (! (ch == 0 || ch == ')') ); }
boolean opt(char ch) { eatws(); if (val.charAt(pos) == ch) { pos++; return true; } return false; }