本文整理汇总了Java中parsii.tokenizer.ParseException类的典型用法代码示例。如果您正苦于以下问题:Java ParseException类的具体用法?Java ParseException怎么用?Java ParseException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParseException类属于parsii.tokenizer包,在下文中一共展示了ParseException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: simple
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void simple() throws ParseException {
assertEquals(-109d, Parser.parse("1 - (10 - -100)").evaluate(), BinaryOperation.EPSILON);
assertEquals(0.01d, Parser.parse("1 / 10 * 10 / 100").evaluate(), BinaryOperation.EPSILON);
assertEquals(-89d, Parser.parse("1 + 10 - 100").evaluate(), BinaryOperation.EPSILON);
assertEquals(91d, Parser.parse("1 - 10 - -100").evaluate(), BinaryOperation.EPSILON);
assertEquals(91d, Parser.parse("1 - 10 + 100").evaluate(), BinaryOperation.EPSILON);
assertEquals(-109d, Parser.parse("1 - (10 + 100)").evaluate(), BinaryOperation.EPSILON);
assertEquals(-89d, Parser.parse("1 + (10 - 100)").evaluate(), BinaryOperation.EPSILON);
assertEquals(100d, Parser.parse("1 / 1 * 100").evaluate(), BinaryOperation.EPSILON);
assertEquals(0.01d, Parser.parse("1 / (1 * 100)").evaluate(), BinaryOperation.EPSILON);
assertEquals(0.01d, Parser.parse("1 * 1 / 100").evaluate(), BinaryOperation.EPSILON);
assertEquals(7d, Parser.parse("3+4").evaluate(), BinaryOperation.EPSILON);
assertEquals(7d, Parser.parse("3 + 4").evaluate(), BinaryOperation.EPSILON);
assertEquals(-1d, Parser.parse("3+ -4").evaluate(), BinaryOperation.EPSILON);
assertEquals(-1d, Parser.parse("3+(-4)").evaluate(), BinaryOperation.EPSILON);
}
示例2: scopes
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void scopes() throws ParseException {
Scope root = new Scope();
Variable a = root.getVariable("a").withValue(1);
Scope subScope1 = new Scope().withParent(root);
Scope subScope2 = new Scope().withParent(root);
Variable b1 = subScope1.getVariable("b").withValue(2);
Variable b2 = subScope2.getVariable("b").withValue(3);
Variable c = root.getVariable("c").withValue(4);
Variable c1 = subScope1.getVariable("c").withValue(5);
assertEquals(c, c1);
Variable d = root.getVariable("d").withValue(9);
Variable d1 = subScope1.create("d").withValue(7);
assertNotEquals(d, d1);
Expression expr1 = Parser.parse("a + b + c + d", subScope1);
Expression expr2 = Parser.parse("a + b + c + d", subScope2);
assertEquals(15d, expr1.evaluate(), BinaryOperation.EPSILON);
assertEquals(18d, expr2.evaluate(), BinaryOperation.EPSILON);
a.setValue(10);
b1.setValue(20);
b2.setValue(30);
c.setValue(40);
c1.setValue(50);
assertEquals(87d, expr1.evaluate(), BinaryOperation.EPSILON);
assertEquals(99d, expr2.evaluate(), BinaryOperation.EPSILON);
}
示例3: parse
import parsii.tokenizer.ParseException; //导入依赖的package包/类
/**
* Parses the given input returning the parsed stylesheet.
*
* @return the AST representation of the parsed input
* @throws ParseException if one or more problems occurred while parsing
*/
public Stylesheet parse() throws ParseException {
while (tokenizer.more()) {
if (tokenizer.current().isKeyword(KEYWORD_IMPORT)) {
// Handle @import
parseImport();
} else if (tokenizer.current().isKeyword(KEYWORD_MIXIN)) {
// Handle @mixin
Mixin mixin = parseMixin();
if (mixin.getName() != null) {
result.addMixin(mixin);
}
} else if (tokenizer.current().isKeyword(KEYWORD_MEDIA)) {
// Handle @media
result.addSection(parseSection(true));
} else if (tokenizer.current().isSpecialIdentifier("$") && tokenizer.next().isSymbol(":")) {
// Handle variable definition
parseVariableDeclaration();
} else {
// Everything else is a "normal" section with selectors and attributes
result.addSection(parseSection(false));
}
}
// Something went wrong? Throw an exception
if (!tokenizer.getProblemCollector().isEmpty()) {
throw ParseException.create(tokenizer.getProblemCollector());
}
return result;
}
示例4: computeMetricFunctions
import parsii.tokenizer.ParseException; //导入依赖的package包/类
public List<MetricFunction> computeMetricFunctions() {
try {
Scope scope = Scope.create();
Set<String> metricTokens = new TreeSet<>(); // can be either metric names or ids ! :-/
// expression parser errors out on variables starting with _
// we're replacing the __COUNT default metric, with an escaped string
// after evaluating, we replace the escaped string back with the original
String modifiedExpressions = expression.replace(COUNT_METRIC, COUNT_METRIC_ESCAPED);
Parser.parse(modifiedExpressions, scope);
metricTokens = scope.getLocalNames();
ArrayList<MetricFunction> metricFunctions = new ArrayList<>();
for (String metricToken : metricTokens) {
Long metricId = null;
MetricConfigDTO metricConfig = null;
String metricDataset = dataset;
DatasetConfigDTO datasetConfig = ThirdEyeUtils.getDatasetConfigFromName(metricDataset);
if (metricToken.equals(COUNT_METRIC_ESCAPED)) {
metricToken = COUNT_METRIC;
} else {
metricId = Long.valueOf(metricToken.replace(MetricConfigBean.DERIVED_METRIC_ID_PREFIX, ""));
metricConfig = ThirdEyeUtils.getMetricConfigFromId(metricId);
if (metricConfig != null) {
metricDataset = metricConfig.getDataset();
}
}
metricFunctions.add(
new MetricFunction(aggFunction, metricToken, metricId, metricDataset, metricConfig, datasetConfig));
}
return metricFunctions;
} catch (ParseException e) {
throw new RuntimeException("Exception parsing expressionString:" + expression, e);
}
}
示例5: parse
import parsii.tokenizer.ParseException; //导入依赖的package包/类
/**
* Parses the expression in <tt>input</tt>
*
* @return the parsed expression
* @throws ParseException if the expression contains one or more errors
*/
protected Expression parse() throws ParseException {
Expression result = expression().simplify();
if (tokenizer.current().isNotEnd()) {
Token token = tokenizer.consume();
errors.add(ParseError.error(token,
String.format("Unexpected token: '%s'. Expected an expression.",
token.getSource())));
}
if (!errors.isEmpty()) {
throw ParseException.create(errors);
}
return result;
}
示例6: precedence
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void precedence() throws ParseException {
// term vs. product
assertEquals(19d, Parser.parse("3+4*4").evaluate(), BinaryOperation.EPSILON);
// product vs. power
assertEquals(20.25d, Parser.parse("3^4/4").evaluate(), BinaryOperation.EPSILON);
// relation vs. product
assertEquals(1d, Parser.parse("3 < 4*4").evaluate(), BinaryOperation.EPSILON);
assertEquals(0d, Parser.parse("3 > 4*4").evaluate(), BinaryOperation.EPSILON);
// brackets
assertEquals(28d, Parser.parse("(3 + 4) * 4").evaluate(), BinaryOperation.EPSILON);
}
示例7: signed
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void signed() throws ParseException {
assertEquals(-2.02, Parser.parse("-2.02").evaluate(), BinaryOperation.EPSILON);
assertEquals(2.02, Parser.parse("+2.02").evaluate(), BinaryOperation.EPSILON);
assertEquals(1.01, Parser.parse("+2.02 + -1.01").evaluate(), BinaryOperation.EPSILON);
assertEquals(-4.03, Parser.parse("-2.02 - +2.01").evaluate(), BinaryOperation.EPSILON);
assertEquals(3.03, Parser.parse("+2.02 + +1.01").evaluate(), BinaryOperation.EPSILON);
}
示例8: startingWithDecimalPoint
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void startingWithDecimalPoint() throws ParseException {
assertEquals(.2, Parser.parse(".2").evaluate(), BinaryOperation.EPSILON);
assertEquals(.2, Parser.parse("+.2").evaluate(), BinaryOperation.EPSILON);
assertEquals(.4, Parser.parse(".2+.2").evaluate(), BinaryOperation.EPSILON);
assertEquals(.4, Parser.parse(".6+-.2").evaluate(), BinaryOperation.EPSILON);
}
示例9: signedParentheses
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void signedParentheses() throws ParseException {
assertEquals(0.2, Parser.parse("-(-0.2)").evaluate(), BinaryOperation.EPSILON);
assertEquals(1.2, Parser.parse("1-(-0.2)").evaluate(), BinaryOperation.EPSILON);
assertEquals(0.8, Parser.parse("1+(-0.2)").evaluate(), BinaryOperation.EPSILON);
assertEquals(2.2, Parser.parse("+(2.2)").evaluate(), BinaryOperation.EPSILON);
}
示例10: signedValueAfterOperand
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void signedValueAfterOperand() throws ParseException {
assertEquals(-1.2, Parser.parse("1+-2.2").evaluate(), BinaryOperation.EPSILON);
assertEquals(3.2, Parser.parse("1++2.2").evaluate(), BinaryOperation.EPSILON);
assertEquals(6 * -1.1, Parser.parse("6*-1.1").evaluate(), BinaryOperation.EPSILON);
assertEquals(6 * 1.1, Parser.parse("6*+1.1").evaluate(), BinaryOperation.EPSILON);
}
示例11: variables
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void variables() throws ParseException {
Scope scope = new Scope();
Variable a = scope.create("a");
Variable b = scope.create("b");
Expression expr = Parser.parse("3*a + 4 * b", scope);
assertEquals(0d, expr.evaluate(), BinaryOperation.EPSILON);
a.setValue(2);
assertEquals(6d, expr.evaluate(), BinaryOperation.EPSILON);
b.setValue(3);
assertEquals(18d, expr.evaluate(), BinaryOperation.EPSILON);
assertEquals(18d, expr.evaluate(), BinaryOperation.EPSILON);
}
示例12: functions
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void functions() throws ParseException {
assertEquals(0d, Parser.parse("1 + sin(-pi) + cos(pi)").evaluate(), BinaryOperation.EPSILON);
assertEquals(4.72038341576d, Parser.parse("tan(sqrt(euler ^ (pi * 3)))").evaluate(), BinaryOperation.EPSILON);
assertEquals(3d, Parser.parse("| 3 - 6 |").evaluate(), BinaryOperation.EPSILON);
assertEquals(3d, Parser.parse("if(3 > 2 && 2 < 3, 2+1, 1+1)").evaluate(), BinaryOperation.EPSILON);
assertEquals(2d, Parser.parse("if(3 < 2 || 2 > 3, 2+1, 1+1)").evaluate(), BinaryOperation.EPSILON);
assertEquals(2d, Parser.parse("min(3,2)").evaluate(), BinaryOperation.EPSILON);
// Test a var arg method...
Parser.registerFunction("avg", new Function() {
@Override
public int getNumberOfArguments() {
return -1;
}
@Override
public double eval(List<Expression> args) {
double avg = 0;
if (args.isEmpty()) {
return avg;
}
for (Expression e : args) {
avg += e.evaluate();
}
return avg / args.size();
}
@Override
public boolean isNaturalFunction() {
return true;
}
});
assertEquals(3.25d, Parser.parse("avg(3,2,1,7)").evaluate(), BinaryOperation.EPSILON);
}
示例13: relationalOperators
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void relationalOperators() throws ParseException {
// Test for Issue with >= and <= operators (#4)
assertEquals(1d, Parser.parse("5 <= 5").evaluate(), BinaryOperation.EPSILON);
assertEquals(1d, Parser.parse("5 >= 5").evaluate(), BinaryOperation.EPSILON);
assertEquals(0d, Parser.parse("5 < 5").evaluate(), BinaryOperation.EPSILON);
assertEquals(0d, Parser.parse("5 > 5").evaluate(), BinaryOperation.EPSILON);
}
示例14: quantifiers
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void quantifiers() throws ParseException {
assertEquals(1000d, Parser.parse("1K").evaluate(), BinaryOperation.EPSILON);
assertEquals(1000d, Parser.parse("1M * 1m").evaluate(), BinaryOperation.EPSILON);
assertEquals(1d, Parser.parse("1n * 1G").evaluate(), BinaryOperation.EPSILON);
assertEquals(1d, Parser.parse("(1M / 1k) * 1m").evaluate(), BinaryOperation.EPSILON);
assertEquals(1d, Parser.parse("1u * 10 k * 1000 m * 0.1 k").evaluate(), BinaryOperation.EPSILON);
}
示例15: getVariables
import parsii.tokenizer.ParseException; //导入依赖的package包/类
@Test
public void getVariables() throws ParseException {
Scope s = new Scope();
Parser.parse("a*b+c", s);
assertTrue(s.getNames().contains("a"));
assertTrue(s.getNames().contains("b"));
assertTrue(s.getNames().contains("c"));
assertFalse(s.getNames().contains("x"));
// pi and euler are always defined...
assertEquals(5, s.getVariables().size());
}