1
|
package eu.dnetlib.springutils.condbean.parser;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
|
9
|
import eu.dnetlib.springutils.condbean.ConditionExpressionParser;
|
10
|
import eu.dnetlib.springutils.condbean.parser.ast.AbstractExpression;
|
11
|
import eu.dnetlib.springutils.condbean.parser.ast.Converter;
|
12
|
import fri.patterns.interpreter.parsergenerator.Lexer;
|
13
|
import fri.patterns.interpreter.parsergenerator.Parser;
|
14
|
import fri.patterns.interpreter.parsergenerator.ParserTables;
|
15
|
import fri.patterns.interpreter.parsergenerator.lexer.LexerException;
|
16
|
import fri.patterns.interpreter.parsergenerator.parsertables.LALRParserTables;
|
17
|
import fri.patterns.interpreter.parsergenerator.parsertables.ParserBuildException;
|
18
|
import fri.patterns.interpreter.parsergenerator.syntax.SyntaxException;
|
19
|
import fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxBuilder;
|
20
|
|
21
|
public class RunccExpressionParser implements ConditionExpressionParser {
|
22
|
|
23
|
private static final Log log = LogFactory //NOPMD
|
24
|
.getLog(RunccExpressionParser.class);
|
25
|
|
26
|
private Parser parser;
|
27
|
private InputStream syntaxInput;
|
28
|
private SyntaxBuilder builder;
|
29
|
private Lexer lexer;
|
30
|
private ParserTables tables;
|
31
|
|
32
|
private CondBeanParser condBeanParser;
|
33
|
|
34
|
@Override
|
35
|
public boolean expressionValue(final String expression) {
|
36
|
try {
|
37
|
parser.setInput(expression);
|
38
|
if (!parser.parse(condBeanParser))
|
39
|
log.fatal("Syntax errors in the expression");
|
40
|
} catch (IOException ioExc) {
|
41
|
log.fatal("Error in evaluating the expression", ioExc);
|
42
|
}
|
43
|
return Converter.toBoolean(((AbstractExpression) parser.getResult()).evaluate());
|
44
|
}
|
45
|
|
46
|
public AbstractExpression getTopRule(final String expression) {
|
47
|
try {
|
48
|
parser.setInput(expression);
|
49
|
if (!parser.parse(new CondBeanParser()))
|
50
|
log.fatal("Syntax errors in the expression");
|
51
|
} catch (IOException ioExc) {
|
52
|
log.fatal("Error in evaluating the expression", ioExc);
|
53
|
}
|
54
|
return (AbstractExpression) parser.getResult();
|
55
|
}
|
56
|
|
57
|
public RunccExpressionParser() {
|
58
|
try {
|
59
|
syntaxInput = CondBeanParser.class.getResourceAsStream("CondBeanParser.syntax");
|
60
|
builder = new SyntaxBuilder(syntaxInput);
|
61
|
lexer = builder.getLexer();
|
62
|
tables = new LALRParserTables(builder.getParserSyntax());
|
63
|
parser = new Parser(tables);
|
64
|
parser.setLexer(lexer);
|
65
|
} catch (SyntaxException syntEx) {
|
66
|
log.fatal("Errors in the syntax specification!", syntEx);
|
67
|
} catch (LexerException lexEx) {
|
68
|
log.fatal("Lexer error!", lexEx);
|
69
|
} catch (ParserBuildException parsBuilEx) {
|
70
|
log.fatal("Cannot create the Parser, errors in the grammar specification!", parsBuilEx);
|
71
|
} catch (IOException ioEx) {
|
72
|
throw new IllegalStateException("Cannot read the grammar file!", ioEx);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
public Parser getParser() {
|
77
|
return parser;
|
78
|
}
|
79
|
|
80
|
public void setParser(final Parser parser) {
|
81
|
this.parser = parser;
|
82
|
}
|
83
|
|
84
|
public InputStream getSyntaxInput() {
|
85
|
return syntaxInput;
|
86
|
}
|
87
|
|
88
|
public void setSyntaxInput(final InputStream syntaxInput) {
|
89
|
this.syntaxInput = syntaxInput;
|
90
|
}
|
91
|
|
92
|
public SyntaxBuilder getBuilder() {
|
93
|
return builder;
|
94
|
}
|
95
|
|
96
|
public void setBuilder(final SyntaxBuilder builder) {
|
97
|
this.builder = builder;
|
98
|
}
|
99
|
|
100
|
public Lexer getLexer() {
|
101
|
return lexer;
|
102
|
}
|
103
|
|
104
|
public void setLexer(final Lexer lexer) {
|
105
|
this.lexer = lexer;
|
106
|
}
|
107
|
|
108
|
public ParserTables getTables() {
|
109
|
return tables;
|
110
|
}
|
111
|
|
112
|
public void setTables(final ParserTables tables) {
|
113
|
this.tables = tables;
|
114
|
}
|
115
|
|
116
|
public CondBeanParser getCondBeanParser() {
|
117
|
return condBeanParser;
|
118
|
}
|
119
|
|
120
|
public void setCondBeanParser(CondBeanParser condBeanParser) {
|
121
|
this.condBeanParser = condBeanParser;
|
122
|
}
|
123
|
|
124
|
}
|