Commit 5e253d8b authored by Martin Spoto's avatar Martin Spoto
Browse files

Implement parsing of lists and clauses (runtime broken tho)

parent f6e28601
......@@ -3,7 +3,7 @@ grammar ProloGraal;
// parser
prolograal :
clause clause* EOF
clause* EOF
;
atom :
......
......@@ -95,18 +95,6 @@ public class ProloGraalBaseListener implements ProloGraalListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitTerm(ProloGraalParser.TermContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterCallable(ProloGraalParser.CallableContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitCallable(ProloGraalParser.CallableContext ctx) { }
/**
* {@inheritDoc}
*
......
......@@ -77,16 +77,6 @@ public interface ProloGraalListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitTerm(ProloGraalParser.TermContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#callable}.
* @param ctx the parse tree
*/
void enterCallable(ProloGraalParser.CallableContext ctx);
/**
* Exit a parse tree produced by {@link ProloGraalParser#callable}.
* @param ctx the parse tree
*/
void exitCallable(ProloGraalParser.CallableContext ctx);
/**
* Enter a parse tree produced by {@link ProloGraalParser#head}.
* @param ctx the parse tree
......
......@@ -4,127 +4,225 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.antlr.v4.runtime.ParserRuleContext;
import ch.heiafr.prolograal.runtime.*;
public class ProloGraalListenerImpl extends ProloGraalBaseListener {
private Stack<ProloGraalClause> clauses = new Stack<>();
private static class DebugStack extends Stack<ProloGraalTerm<?>> {
@Override
public ProloGraalTerm<?> push(ProloGraalTerm<?> item) {
System.out.println("Pushing : " + item.toString());
return super.push(item);
}
public void debug() {
System.out.println(clauses);
@Override
public ProloGraalTerm<?> pop() {
ProloGraalTerm<?> e = super.pop();
System.out.println("Popping off : " + e.toString());
return e;
}
}
@Override
public void enterProlograal(ProloGraalParser.ProlograalContext ctx) {
}
@Override
public void exitProlograal(ProloGraalParser.ProlograalContext ctx) {
}
@Override
public void enterAtom(ProloGraalParser.AtomContext ctx) {
}
@Override
public void exitAtom(ProloGraalParser.AtomContext ctx) {
}
@Override
public void enterNumber(ProloGraalParser.NumberContext ctx) {
}
@Override
public void exitNumber(ProloGraalParser.NumberContext ctx) {
}
private Stack<ProloGraalTerm<?>> elements = new DebugStack();
private Stack<ProloGraalClause> clauses = new Stack<>();
@Override
public void enterVariable(ProloGraalParser.VariableContext ctx) {
public void debug() {
System.out.println(clauses);
}
@Override
public void exitVariable(ProloGraalParser.VariableContext ctx) {
private void throwParseError(ParserRuleContext ctx, String message) {
ProloGraalParserImpl.throwParseError(ProloGraalParserImpl.source, ctx.getStart().getLine(), 0, ctx.getStart(), message);
}
/*
prolograal :
clause* EOF
;
*/
@Override
public void enterFunctor(ProloGraalParser.FunctorContext ctx) {
public void enterProlograal(ProloGraalParser.ProlograalContext ctx) {
}
@Override
public void exitFunctor(ProloGraalParser.FunctorContext ctx) {
public void exitProlograal(ProloGraalParser.ProlograalContext ctx) {
}
/*
clause :
fact |
composedClause
;
*/
@Override
public void enterComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
public void enterClause(ProloGraalParser.ClauseContext ctx) {
clauses.push(new ProloGraalClause());
}
/*
fact :
head TERMINATOR
;
*/
@Override
public void exitComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
public void enterFact(ProloGraalParser.FactContext ctx) {
}
/*
composedClause :
head CLAUSE_MARKER goal (SEPARATOR goal)* TERMINATOR
;
*/
@Override
public void enterTerm(ProloGraalParser.TermContext ctx) {
public void enterComposedClause(ProloGraalParser.ComposedClauseContext ctx) {
}
/*
goal :
atom |
composedTerm
;
*/
@Override
public void exitTerm(ProloGraalParser.TermContext ctx) {
public void enterGoal(ProloGraalParser.GoalContext ctx) {
}
@Override
public void enterFact(ProloGraalParser.FactContext ctx) {
public void exitGoal(ProloGraalParser.GoalContext ctx) {
clauses.peek().addGoal(elements.pop());
}
/*
head :
atom |
composedTerm
;
*/
@Override
public void exitFact(ProloGraalParser.FactContext ctx) {
public void enterHead(ProloGraalParser.HeadContext ctx) {
}
@Override
public void enterGoal(ProloGraalParser.GoalContext ctx) {
public void exitHead(ProloGraalParser.HeadContext ctx) {
clauses.peek().setHead(elements.pop());
}
/*
atom :
ATOM |
LIST_START LIST_END // empty list
;
*/
@Override
public void exitGoal(ProloGraalParser.GoalContext ctx) {
public void enterAtom(ProloGraalParser.AtomContext ctx) {
ProloGraalAtom atom = new ProloGraalAtom(ctx.getText());
elements.push(atom);
}
@Override
public void enterCallable(ProloGraalParser.CallableContext ctx) {
public void enterNumber(ProloGraalParser.NumberContext ctx) {
String n = ctx.getText();
try {
elements.push(new ProloGraalIntegerNumber(Integer.parseInt(n)));
} catch (NumberFormatException ex) {
elements.push(new ProloGraalDoubleNumber(Double.parseDouble(n)));
}
}
@Override
public void exitCallable(ProloGraalParser.CallableContext ctx) {
public void enterVariable(ProloGraalParser.VariableContext ctx) {
ProloGraalVariable variable = new ProloGraalVariable(ctx.getText());
if(clauses.peek().getVariables().containsKey(variable)) {
elements.push(clauses.peek().getVariables().get(variable)); // add a reference to the previously added one
} else {
elements.push(variable);
clauses.peek().getVariables().put(variable, variable);
}
}
/*
composedTerm :
functor ('(' term (',' term)* ')')
;
*/
@Override
public void enterHead(ProloGraalParser.HeadContext ctx) {
public void enterComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
elements.push(new ProloGraalStructure());
}
@Override
public void exitHead(ProloGraalParser.HeadContext ctx) {
}
public void exitComposedTerm(ProloGraalParser.ComposedTermContext ctx) {
List<ProloGraalTerm<?>> subterms = new ArrayList<>();
while(true){
while(!(elements.peek() instanceof ProloGraalStructure)) {
subterms.add(elements.pop());
}
if(((ProloGraalStructure)elements.peek()).getArity() > 0) { // element is a composed term already handled
subterms.add(elements.pop());
} else {
break;
}
}
ProloGraalStructure struct = (ProloGraalStructure) elements.peek();
for(int i = subterms.size()-1; i >= 0; i--) {
struct.addSubterm(subterms.get(i));
}
}
/*
functor :
atom
;
*/
@Override
public void enterComposedClause(ProloGraalParser.ComposedClauseContext ctx) {
public void enterFunctor(ProloGraalParser.FunctorContext ctx) {
}
@Override
public void exitComposedClause(ProloGraalParser.ComposedClauseContext ctx) {
public void exitFunctor(ProloGraalParser.FunctorContext ctx) {
try {
ProloGraalAtom functor = (ProloGraalAtom) elements.pop();
ProloGraalStructure struct = (ProloGraalStructure) elements.peek();
struct.setFunctor(functor);
} catch(ClassCastException ex) {
throwParseError(ctx, "Invalid state : " + ex.getLocalizedMessage());
}
}
@Override
public void enterClause(ProloGraalParser.ClauseContext ctx) {
public void enterTerm(ProloGraalParser.TermContext ctx) {
}
@Override
public void exitClause(ProloGraalParser.ClauseContext ctx) {
public void exitTerm(ProloGraalParser.TermContext ctx) {
}
@Override
public void enterList(ProloGraalParser.ListContext ctx) {
elements.push(new ProloGraalList());
}
@Override
public void exitList(ProloGraalParser.ListContext ctx) {
List<ProloGraalTerm<?>> items = new ArrayList<>();
while(true) {
while(!(elements.peek() instanceof ProloGraalList)) {
items.add(elements.pop());
}
if(((ProloGraalList)elements.peek()).size() > 0) { // element is a list already handled
items.add(elements.pop());
} else {
break;
}
}
ProloGraalList list = (ProloGraalList) elements.peek();
for(int i = items.size()-1; i >= 0; i--) {
list.addItem(items.get(i));
}
}
}
\ No newline at end of file
......@@ -22,13 +22,12 @@ public class ProloGraalParser extends Parser {
NUMBER=13;
public static final int
RULE_prolograal = 0, RULE_atom = 1, RULE_number = 2, RULE_variable = 3,
RULE_functor = 4, RULE_composedTerm = 5, RULE_term = 6, RULE_callable = 7,
RULE_head = 8, RULE_fact = 9, RULE_goal = 10, RULE_composedClause = 11,
RULE_clause = 12, RULE_list = 13;
RULE_functor = 4, RULE_composedTerm = 5, RULE_term = 6, RULE_head = 7,
RULE_fact = 8, RULE_goal = 9, RULE_composedClause = 10, RULE_clause = 11,
RULE_list = 12;
public static final String[] ruleNames = {
"prolograal", "atom", "number", "variable", "functor", "composedTerm",
"term", "callable", "head", "fact", "goal", "composedClause", "clause",
"list"
"term", "head", "fact", "goal", "composedClause", "clause", "list"
};
private static final String[] _LITERAL_NAMES = {
......@@ -89,13 +88,13 @@ public class ProloGraalParser extends Parser {
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
public static class ProlograalContext extends ParserRuleContext {
public TerminalNode EOF() { return getToken(ProloGraalParser.EOF, 0); }
public List<ClauseContext> clause() {
return getRuleContexts(ClauseContext.class);
}
public ClauseContext clause(int i) {
return getRuleContext(ClauseContext.class,i);
}
public TerminalNode EOF() { return getToken(ProloGraalParser.EOF, 0); }
public ProlograalContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
......@@ -117,23 +116,21 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(28);
clause();
setState(32);
setState(29);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==LIST_START || _la==ATOM) {
{
{
setState(29);
setState(26);
clause();
}
}
setState(34);
setState(31);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(35);
setState(32);
match(EOF);
}
}
......@@ -170,22 +167,22 @@ public class ProloGraalParser extends Parser {
AtomContext _localctx = new AtomContext(_ctx, getState());
enterRule(_localctx, 2, RULE_atom);
try {
setState(40);
setState(37);
_errHandler.sync(this);
switch (_input.LA(1)) {
case ATOM:
enterOuterAlt(_localctx, 1);
{
setState(37);
setState(34);
match(ATOM);
}
break;
case LIST_START:
enterOuterAlt(_localctx, 2);
{
setState(38);
setState(35);
match(LIST_START);
setState(39);
setState(36);
match(LIST_END);
}
break;
......@@ -226,7 +223,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(42);
setState(39);
match(NUMBER);
}
}
......@@ -263,7 +260,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(44);
setState(41);
match(VARIABLE);
}
}
......@@ -302,7 +299,7 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(46);
setState(43);
atom();
}
}
......@@ -348,30 +345,30 @@ public class ProloGraalParser extends Parser {
try {
enterOuterAlt(_localctx, 1);
{
setState(48);
setState(45);
functor();
{
setState(49);
setState(46);
match(T__0);
setState(50);
setState(47);
term();
setState(55);
setState(52);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==SEPARATOR) {
{
{
setState(51);
setState(48);
match(SEPARATOR);
setState(52);
setState(49);
term();
}
}
setState(57);
setState(54);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(58);
setState(55);
match(T__1);
}
}
......@@ -421,41 +418,41 @@ public class ProloGraalParser extends Parser {
TermContext _localctx = new TermContext(_ctx, getState());
enterRule(_localctx, 12, RULE_term);
try {
setState(65);
setState(62);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(60);
setState(57);
composedTerm();
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(61);
setState(58);
atom();
}
break;
case 3:
enterOuterAlt(_localctx, 3);
{
setState(62);
setState(59);
number();
}
break;
case 4:
enterOuterAlt(_localctx, 4);
{
setState(63);
setState(60);
variable();
}
break;
case 5:
enterOuterAlt(_localctx, 5);
{
setState(64);
setState(61);
list();
}
break;
......@@ -472,45 +469,45 @@ public class ProloGraalParser extends Parser {
return _localctx;
}
public static class CallableContext extends ParserRuleContext {
public static class HeadContext extends ParserRuleContext {
public AtomContext atom() {
return getRuleContext(AtomContext.class,0);
}
public ComposedTermContext composedTerm() {
return getRuleContext(ComposedTermContext.class,0);
}
public CallableContext(ParserRuleContext parent, int invokingState) {
public HeadContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_callable; }
@Override public int getRuleIndex() { return RULE_head; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).enterCallable(this);
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).enterHead(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).exitCallable(this);
if ( listener instanceof ProloGraalListener ) ((ProloGraalListener)listener).exitHead(this);
}
}
public final CallableContext callable() throws RecognitionException {
CallableContext _localctx = new CallableContext(_ctx, getState());
enterRule(_localctx, 14, RULE_callable);
public final HeadContext head() throws RecognitionException {
HeadContext _localctx = new HeadContext(_ctx, getState());
enterRule(_localctx, 14, RULE_head);
try {
setState(69);
setState(66);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(67);
setState(64);
atom();
}
break;
case 2: