RandomSec/main/src/com/google/refine/expr/MetaParser.java
David Huynh 1de5e7c00e Renamed package gel to grel.
Replaced gel with grel in other places in the code base while maintaining backward compatibility.
Changed layout in expression preview dialog to accommodate long GREL name.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1442 7d457c2a-affb-35e4-300a-418c747d4874
2010-10-07 05:19:35 +00:00

122 lines
4.1 KiB
Java

package com.google.refine.expr;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import clojure.lang.IFn;
import com.google.refine.grel.Parser;
abstract public class MetaParser {
static public class LanguageInfo {
final public String name;
final public LanguageSpecificParser parser;
final public String defaultExpression;
LanguageInfo(String name, LanguageSpecificParser parser, String defaultExpression) {
this.name = name;
this.parser = parser;
this.defaultExpression = defaultExpression;
}
}
static protected Map<String, LanguageInfo> s_languages;
static {
s_languages = new HashMap<String, LanguageInfo>();
registerLanguageParser("grel", "Google Refine Expression Language (GREL)", new LanguageSpecificParser() {
@Override
public Evaluable parse(String s) throws ParsingException {
return parseGREL(s);
}
}, "value");
registerLanguageParser("clojure", "Clojure", new LanguageSpecificParser() {
@Override
public Evaluable parse(String s) throws ParsingException {
try {
IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader(
"(fn [value cell cells row rowIndex] " + s + ")"
));
return new Evaluable() {
private IFn _fn;
public Evaluable init(IFn fn) {
_fn = fn;
return this;
}
public Object evaluate(Properties bindings) {
try {
return _fn.invoke(
bindings.get("value"),
bindings.get("cell"),
bindings.get("cells"),
bindings.get("row"),
bindings.get("rowIndex")
);
} catch (Exception e) {
return new EvalError(e.getMessage());
}
}
}.init(fn);
} catch (Exception e) {
throw new ParsingException(e.getMessage());
}
}
}, "value");
}
static public void registerLanguageParser(String languagePrefix, String name, LanguageSpecificParser parser, String defaultExpression) {
s_languages.put(languagePrefix, new LanguageInfo(name, parser, defaultExpression));
}
static public LanguageInfo getLanguageInfo(String languagePrefix) {
return s_languages.get(languagePrefix.toLowerCase());
}
static public Set<String> getLanguagePrefixes() {
return s_languages.keySet();
}
/**
* Parse an expression that might have a language prefix into an Evaluable.
* Expressions without valid prefixes or without any prefix are assumed to be
* GREL expressions.
*
* @param s
* @return
* @throws ParsingException
*/
static public Evaluable parse(String s) throws ParsingException {
String language = "grel";
int colon = s.indexOf(':');
if (colon >= 0) {
language = s.substring(0, colon).toLowerCase();
if ("gel".equals(language)) {
language = "grel";
}
}
LanguageInfo info = s_languages.get(language.toLowerCase());
if (info != null) {
return info.parser.parse(s.substring(colon + 1));
} else {
return parseGREL(s);
}
}
static protected Evaluable parseGREL(String s) throws ParsingException {
Parser parser = new Parser(s);
return parser.getExpression();
}
}