diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TestTypedefParser.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TestTypedefParser.java index 5835f1e08..f6b3cf18e 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TestTypedefParser.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TestTypedefParser.java @@ -4,6 +4,9 @@ import dk.camelot64.kickc.model.CompileError; import org.antlr.v4.runtime.*; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + import static org.junit.Assert.assertEquals; @@ -11,15 +14,19 @@ public class TestTypedefParser { @Test public void testExprParser() { + + List typedefs = new ArrayList<>(); + // a & b are not types - resolving to values - assertEquals("and(val:a,val:b)", parseExprTypedef("a&b")); + assertEquals("and(val:a,val:b);", parseExprTypedef("a&b;")); // a & b are not types - resolving to values even with added parenthesis (looking like a cast) - assertEquals("and((val:a),val:b)", parseExprTypedef("(a)&b")); + assertEquals("and((val:a),val:b);", parseExprTypedef("(a)&b;")); // char is a simple type - resolving to cast of simple type - assertEquals("cast(simpletype:char,addressof(val:b))", parseExprTypedef("(char)&b")); - // TODO: Fix typedef identification during lexer phase! + assertEquals("cast(simpletype:char,addressof(val:b));", parseExprTypedef("(char)&b;")); // T is typedeffed - so this should resolve to a cast of typedef - assertEquals("cast(typedef:T,addressof(val:b))", parseExprTypedef("(T)&b")); + assertEquals("typedef(simpletype:char,T);cast(typedef:T,addressof(val:b));", parseExprTypedef("typedef char T; (T)&b;")); + // T is not typedeffed - so this should resolve to an AND + assertEquals("typedef(simpletype:char,V);and((val:T),val:b);", parseExprTypedef("typedef char V; (T)&b;")); } /** @@ -60,7 +67,7 @@ public class TestTypedefParser { }); TypedefPrinter printVisitor = new TypedefPrinter(); - printVisitor.visit(parser.expr()); + printVisitor.visit(parser.stmtSeq()); return printVisitor.getOut().toString(); } @@ -72,6 +79,31 @@ public class TestTypedefParser { return out; } + @Override + public Object visitStmtSeq(TypedefParser.StmtSeqContext ctx) { + for(TypedefParser.StmtContext stmtContext : ctx.stmt()) { + this.visit(stmtContext); + out.append(";"); + } + return null; + } + + @Override + public Object visitStmtTypeDef(TypedefParser.StmtTypeDefContext ctx) { + out.append("typedef("); + this.visit(ctx.typeName()); + out.append(","); + out.append(ctx.IDENTIFIER().getText()); + out.append(")"); + return null; + } + + @Override + public Object visitStmtExpr(TypedefParser.StmtExprContext ctx) { + this.visit(ctx.expr()); + return null; + } + @Override public Object visitExprValueName(TypedefParser.ExprValueNameContext ctx) { out.append("val:").append(ctx.getText()); diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 b/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 index c82ab7177..d4f710f7c 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 @@ -11,6 +11,24 @@ grammar Typedef; +@parser::header { + import java.util.ArrayList; + import java.util.List; +} + +@parser::members { + static List typedefs = new ArrayList<>(); +} + +stmtSeq + : { typedefs = new ArrayList<>(); } stmt* + ; + +stmt + : 'typedef' typeName IDENTIFIER ';' { typedefs.add($IDENTIFIER.text);} #stmtTypeDef + | expr ';' #stmtExpr + ; + expr : '(' typeName ')' expr #exprCast | IDENTIFIER #exprValueName @@ -25,8 +43,8 @@ typeName ; SIMPLETYPE: 'char' | 'int'; -IDENTIFIER: [a-zA-Z_]+ {!getText().equals("T")}?; -TYPEIDENTIFIER: [a-zA-Z_]+ {getText().equals("T")}?; +IDENTIFIER: [a-zA-Z_]+ {!TypedefParser.typedefs.contains(getText())}?; +TYPEIDENTIFIER: [a-zA-Z_]+ {TypedefParser.typedefs.contains(getText())}?; WHITESPACE : [ \t\r\n]+ -> skip diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.tokens b/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.tokens index 3a61615e1..65648e03f 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.tokens +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.tokens @@ -1,10 +1,14 @@ T__0=1 T__1=2 T__2=3 -SIMPLETYPE=4 -IDENTIFIER=5 -TYPEIDENTIFIER=6 -WHITESPACE=7 -'('=1 -')'=2 -'&'=3 +T__3=4 +T__4=5 +SIMPLETYPE=6 +IDENTIFIER=7 +TYPEIDENTIFIER=8 +WHITESPACE=9 +'typedef'=1 +';'=2 +'('=3 +')'=4 +'&'=5 diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseListener.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseListener.java index edf5c8199..6f5d08452 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseListener.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseListener.java @@ -1,6 +1,10 @@ // Generated from /Users/jespergravgaard/c64/kickc/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 by ANTLR 4.7 package dk.camelot64.kickc.parsing.typedef; + import java.util.ArrayList; + import java.util.List; + + import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.TerminalNode; @@ -11,6 +15,42 @@ import org.antlr.v4.runtime.tree.TerminalNode; * of the available methods. */ public class TypedefBaseListener implements TypedefListener { + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterStmtSeq(TypedefParser.StmtSeqContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitStmtSeq(TypedefParser.StmtSeqContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterStmtTypeDef(TypedefParser.StmtTypeDefContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitStmtTypeDef(TypedefParser.StmtTypeDefContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterStmtExpr(TypedefParser.StmtExprContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitStmtExpr(TypedefParser.StmtExprContext ctx) { } /** * {@inheritDoc} * diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseVisitor.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseVisitor.java index 4f1bb996f..4cd44de29 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseVisitor.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefBaseVisitor.java @@ -1,5 +1,9 @@ // Generated from /Users/jespergravgaard/c64/kickc/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 by ANTLR 4.7 package dk.camelot64.kickc.parsing.typedef; + + import java.util.ArrayList; + import java.util.List; + import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** @@ -11,6 +15,27 @@ import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; * operations with no return type. */ public class TypedefBaseVisitor extends AbstractParseTreeVisitor implements TypedefVisitor { + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitStmtSeq(TypedefParser.StmtSeqContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitStmtTypeDef(TypedefParser.StmtTypeDefContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitStmtExpr(TypedefParser.StmtExprContext ctx) { return visitChildren(ctx); } /** * {@inheritDoc} * diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.java index 4ec10d26d..5a6519f0b 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.java @@ -17,8 +17,8 @@ public class TypedefLexer extends Lexer { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - T__0=1, T__1=2, T__2=3, SIMPLETYPE=4, IDENTIFIER=5, TYPEIDENTIFIER=6, - WHITESPACE=7; + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, SIMPLETYPE=6, IDENTIFIER=7, TYPEIDENTIFIER=8, + WHITESPACE=9; public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; @@ -28,15 +28,15 @@ public class TypedefLexer extends Lexer { }; public static final String[] ruleNames = { - "T__0", "T__1", "T__2", "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", + "T__0", "T__1", "T__2", "T__3", "T__4", "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", "WHITESPACE" }; private static final String[] _LITERAL_NAMES = { - null, "'('", "')'", "'&'" + null, "'typedef'", "';'", "'('", "')'", "'&'" }; private static final String[] _SYMBOLIC_NAMES = { - null, null, null, null, "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", + null, null, null, null, null, null, "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", "WHITESPACE" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -99,9 +99,9 @@ public class TypedefLexer extends Lexer { @Override public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { - case 4: + case 6: return IDENTIFIER_sempred((RuleContext)_localctx, predIndex); - case 5: + case 7: return TYPEIDENTIFIER_sempred((RuleContext)_localctx, predIndex); } return true; @@ -109,34 +109,37 @@ public class TypedefLexer extends Lexer { private boolean IDENTIFIER_sempred(RuleContext _localctx, int predIndex) { switch (predIndex) { case 0: - return !getText().equals("T"); + return !TypedefParser.typedefs.contains(getText()); } return true; } private boolean TYPEIDENTIFIER_sempred(RuleContext _localctx, int predIndex) { switch (predIndex) { case 1: - return getText().equals("T"); + return TypedefParser.typedefs.contains(getText()); } return true; } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\t\65\b\1\4\2\t\2"+ - "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\3\2\3\2\3\3\3\3\3\4\3"+ - "\4\3\5\3\5\3\5\3\5\3\5\3\5\3\5\5\5\37\n\5\3\6\6\6\"\n\6\r\6\16\6#\3\6"+ - "\3\6\3\7\6\7)\n\7\r\7\16\7*\3\7\3\7\3\b\6\b\60\n\b\r\b\16\b\61\3\b\3\b"+ - "\2\2\t\3\3\5\4\7\5\t\6\13\7\r\b\17\t\3\2\4\5\2C\\aac|\5\2\13\f\17\17\""+ - "\"\28\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r"+ - "\3\2\2\2\2\17\3\2\2\2\3\21\3\2\2\2\5\23\3\2\2\2\7\25\3\2\2\2\t\36\3\2"+ - "\2\2\13!\3\2\2\2\r(\3\2\2\2\17/\3\2\2\2\21\22\7*\2\2\22\4\3\2\2\2\23\24"+ - "\7+\2\2\24\6\3\2\2\2\25\26\7(\2\2\26\b\3\2\2\2\27\30\7e\2\2\30\31\7j\2"+ - "\2\31\32\7c\2\2\32\37\7t\2\2\33\34\7k\2\2\34\35\7p\2\2\35\37\7v\2\2\36"+ - "\27\3\2\2\2\36\33\3\2\2\2\37\n\3\2\2\2 \"\t\2\2\2! \3\2\2\2\"#\3\2\2\2"+ - "#!\3\2\2\2#$\3\2\2\2$%\3\2\2\2%&\6\6\2\2&\f\3\2\2\2\')\t\2\2\2(\'\3\2"+ - "\2\2)*\3\2\2\2*(\3\2\2\2*+\3\2\2\2+,\3\2\2\2,-\6\7\3\2-\16\3\2\2\2.\60"+ - "\t\3\2\2/.\3\2\2\2\60\61\3\2\2\2\61/\3\2\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\2/.\3\2\2\2\60\61\3\2\2\2\61/\3"+ + "\2\2\2\61\62\3\2\2\2\62\63\3\2\2\2\63\64\6\b\2\2\64\20\3\2\2\2\65\67\t"+ + "\2\2\2\66\65\3\2\2\2\678\3\2\2\28\66\3\2\2\289\3\2\2\29:\3\2\2\2:;\6\t"+ + "\3\2;\22\3\2\2\2<>\t\3\2\2=<\3\2\2\2>?\3\2\2\2?=\3\2\2\2?@\3\2\2\2@A\3"+ + "\2\2\2AB\b\n\2\2B\24\3\2\2\2\7\2,\618?\3\b\2\2"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.tokens b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.tokens index 3a61615e1..65648e03f 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.tokens +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefLexer.tokens @@ -1,10 +1,14 @@ T__0=1 T__1=2 T__2=3 -SIMPLETYPE=4 -IDENTIFIER=5 -TYPEIDENTIFIER=6 -WHITESPACE=7 -'('=1 -')'=2 -'&'=3 +T__3=4 +T__4=5 +SIMPLETYPE=6 +IDENTIFIER=7 +TYPEIDENTIFIER=8 +WHITESPACE=9 +'typedef'=1 +';'=2 +'('=3 +')'=4 +'&'=5 diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefListener.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefListener.java index 4c3708809..c1b1a138f 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefListener.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefListener.java @@ -1,5 +1,9 @@ // Generated from /Users/jespergravgaard/c64/kickc/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 by ANTLR 4.7 package dk.camelot64.kickc.parsing.typedef; + + import java.util.ArrayList; + import java.util.List; + import org.antlr.v4.runtime.tree.ParseTreeListener; /** @@ -7,6 +11,40 @@ import org.antlr.v4.runtime.tree.ParseTreeListener; * {@link TypedefParser}. */ public interface TypedefListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link TypedefParser#stmtSeq}. + * @param ctx the parse tree + */ + void enterStmtSeq(TypedefParser.StmtSeqContext ctx); + /** + * Exit a parse tree produced by {@link TypedefParser#stmtSeq}. + * @param ctx the parse tree + */ + void exitStmtSeq(TypedefParser.StmtSeqContext ctx); + /** + * Enter a parse tree produced by the {@code stmtTypeDef} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + */ + void enterStmtTypeDef(TypedefParser.StmtTypeDefContext ctx); + /** + * Exit a parse tree produced by the {@code stmtTypeDef} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + */ + void exitStmtTypeDef(TypedefParser.StmtTypeDefContext ctx); + /** + * Enter a parse tree produced by the {@code stmtExpr} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + */ + void enterStmtExpr(TypedefParser.StmtExprContext ctx); + /** + * Exit a parse tree produced by the {@code stmtExpr} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + */ + void exitStmtExpr(TypedefParser.StmtExprContext ctx); /** * Enter a parse tree produced by the {@code exprValueName} * labeled alternative in {@link TypedefParser#expr}. diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefParser.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefParser.java index f84365169..6ea068034 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefParser.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefParser.java @@ -1,5 +1,9 @@ // Generated from /Users/jespergravgaard/c64/kickc/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 by ANTLR 4.7 package dk.camelot64.kickc.parsing.typedef; + + import java.util.ArrayList; + import java.util.List; + import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.*; @@ -17,19 +21,19 @@ public class TypedefParser extends Parser { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - T__0=1, T__1=2, T__2=3, SIMPLETYPE=4, IDENTIFIER=5, TYPEIDENTIFIER=6, - WHITESPACE=7; + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, SIMPLETYPE=6, IDENTIFIER=7, TYPEIDENTIFIER=8, + WHITESPACE=9; public static final int - RULE_expr = 0, RULE_typeName = 1; + RULE_stmtSeq = 0, RULE_stmt = 1, RULE_expr = 2, RULE_typeName = 3; public static final String[] ruleNames = { - "expr", "typeName" + "stmtSeq", "stmt", "expr", "typeName" }; private static final String[] _LITERAL_NAMES = { - null, "'('", "')'", "'&'" + null, "'typedef'", "';'", "'('", "')'", "'&'" }; private static final String[] _SYMBOLIC_NAMES = { - null, null, null, null, "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", + null, null, null, null, null, null, "SIMPLETYPE", "IDENTIFIER", "TYPEIDENTIFIER", "WHITESPACE" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -77,10 +81,175 @@ public class TypedefParser extends Parser { @Override public ATN getATN() { return _ATN; } + + static List typedefs = new ArrayList<>(); + public TypedefParser(TokenStream input) { super(input); _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); } + public static class StmtSeqContext extends ParserRuleContext { + public List stmt() { + return getRuleContexts(StmtContext.class); + } + public StmtContext stmt(int i) { + return getRuleContext(StmtContext.class,i); + } + public StmtSeqContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_stmtSeq; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).enterStmtSeq(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).exitStmtSeq(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof TypedefVisitor ) return ((TypedefVisitor)visitor).visitStmtSeq(this); + else return visitor.visitChildren(this); + } + } + + public final StmtSeqContext stmtSeq() throws RecognitionException { + StmtSeqContext _localctx = new StmtSeqContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_stmtSeq); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + typedefs = new ArrayList<>(); + setState(12); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << T__2) | (1L << T__4) | (1L << IDENTIFIER))) != 0)) { + { + { + setState(9); + stmt(); + } + } + setState(14); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class StmtContext extends ParserRuleContext { + public StmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_stmt; } + + public StmtContext() { } + public void copyFrom(StmtContext ctx) { + super.copyFrom(ctx); + } + } + public static class StmtExprContext extends StmtContext { + public ExprContext expr() { + return getRuleContext(ExprContext.class,0); + } + public StmtExprContext(StmtContext ctx) { copyFrom(ctx); } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).enterStmtExpr(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).exitStmtExpr(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof TypedefVisitor ) return ((TypedefVisitor)visitor).visitStmtExpr(this); + else return visitor.visitChildren(this); + } + } + public static class StmtTypeDefContext extends StmtContext { + public Token IDENTIFIER; + public TypeNameContext typeName() { + return getRuleContext(TypeNameContext.class,0); + } + public TerminalNode IDENTIFIER() { return getToken(TypedefParser.IDENTIFIER, 0); } + public StmtTypeDefContext(StmtContext ctx) { copyFrom(ctx); } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).enterStmtTypeDef(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof TypedefListener ) ((TypedefListener)listener).exitStmtTypeDef(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof TypedefVisitor ) return ((TypedefVisitor)visitor).visitStmtTypeDef(this); + else return visitor.visitChildren(this); + } + } + + public final StmtContext stmt() throws RecognitionException { + StmtContext _localctx = new StmtContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_stmt); + try { + setState(24); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__0: + _localctx = new StmtTypeDefContext(_localctx); + enterOuterAlt(_localctx, 1); + { + setState(15); + match(T__0); + setState(16); + typeName(); + setState(17); + ((StmtTypeDefContext)_localctx).IDENTIFIER = match(IDENTIFIER); + setState(18); + match(T__1); + typedefs.add((((StmtTypeDefContext)_localctx).IDENTIFIER!=null?((StmtTypeDefContext)_localctx).IDENTIFIER.getText():null)); + } + break; + case T__2: + case T__4: + case IDENTIFIER: + _localctx = new StmtExprContext(_localctx); + enterOuterAlt(_localctx, 2); + { + setState(21); + expr(0); + setState(22); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + public static class ExprContext extends ParserRuleContext { public ExprContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); @@ -201,28 +370,28 @@ public class TypedefParser extends Parser { int _parentState = getState(); ExprContext _localctx = new ExprContext(_ctx, _parentState); ExprContext _prevctx = _localctx; - int _startState = 0; - enterRecursionRule(_localctx, 0, RULE_expr, _p); + int _startState = 4; + enterRecursionRule(_localctx, 4, RULE_expr, _p); try { int _alt; enterOuterAlt(_localctx, 1); { - setState(17); + setState(39); _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,0,_ctx) ) { + switch ( getInterpreter().adaptivePredict(_input,2,_ctx) ) { case 1: { _localctx = new ExprCastContext(_localctx); _ctx = _localctx; _prevctx = _localctx; - setState(5); - match(T__0); - setState(6); + setState(27); + match(T__2); + setState(28); typeName(); - setState(7); - match(T__1); - setState(8); + setState(29); + match(T__3); + setState(30); expr(5); } break; @@ -231,7 +400,7 @@ public class TypedefParser extends Parser { _localctx = new ExprValueNameContext(_localctx); _ctx = _localctx; _prevctx = _localctx; - setState(10); + setState(32); match(IDENTIFIER); } break; @@ -240,12 +409,12 @@ public class TypedefParser extends Parser { _localctx = new ExprParenthesisContext(_localctx); _ctx = _localctx; _prevctx = _localctx; - setState(11); - match(T__0); - setState(12); + setState(33); + match(T__2); + setState(34); expr(0); - setState(13); - match(T__1); + setState(35); + match(T__3); } break; case 4: @@ -253,17 +422,17 @@ public class TypedefParser extends Parser { _localctx = new ExprAddressOfContext(_localctx); _ctx = _localctx; _prevctx = _localctx; - setState(15); - match(T__2); - setState(16); + setState(37); + match(T__4); + setState(38); expr(2); } break; } _ctx.stop = _input.LT(-1); - setState(24); + setState(46); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,1,_ctx); + _alt = getInterpreter().adaptivePredict(_input,3,_ctx); while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { if ( _alt==1 ) { if ( _parseListeners!=null ) triggerExitRuleEvent(); @@ -272,18 +441,18 @@ public class TypedefParser extends Parser { { _localctx = new ExprAndContext(new ExprContext(_parentctx, _parentState)); pushNewRecursionContext(_localctx, _startState, RULE_expr); - setState(19); + setState(41); if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(20); - match(T__2); - setState(21); + setState(42); + match(T__4); + setState(43); expr(2); } } } - setState(26); + setState(48); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,1,_ctx); + _alt = getInterpreter().adaptivePredict(_input,3,_ctx); } } } @@ -346,16 +515,16 @@ public class TypedefParser extends Parser { public final TypeNameContext typeName() throws RecognitionException { TypeNameContext _localctx = new TypeNameContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_typeName); + enterRule(_localctx, 6, RULE_typeName); try { - setState(29); + setState(51); _errHandler.sync(this); switch (_input.LA(1)) { case SIMPLETYPE: _localctx = new TypeNameSimpleContext(_localctx); enterOuterAlt(_localctx, 1); { - setState(27); + setState(49); match(SIMPLETYPE); } break; @@ -363,7 +532,7 @@ public class TypedefParser extends Parser { _localctx = new TypeNameTypedefContext(_localctx); enterOuterAlt(_localctx, 2); { - setState(28); + setState(50); match(TYPEIDENTIFIER); } break; @@ -384,7 +553,7 @@ public class TypedefParser extends Parser { public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch (ruleIndex) { - case 0: + case 2: return expr_sempred((ExprContext)_localctx, predIndex); } return true; @@ -398,16 +567,21 @@ public class TypedefParser extends Parser { } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\t\"\4\2\t\2\4\3\t"+ - "\3\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\5\2\24\n\2\3\2"+ - "\3\2\3\2\7\2\31\n\2\f\2\16\2\34\13\2\3\3\3\3\5\3 \n\3\3\3\2\3\2\4\2\4"+ - "\2\2\2$\2\23\3\2\2\2\4\37\3\2\2\2\6\7\b\2\1\2\7\b\7\3\2\2\b\t\5\4\3\2"+ - "\t\n\7\4\2\2\n\13\5\2\2\7\13\24\3\2\2\2\f\24\7\7\2\2\r\16\7\3\2\2\16\17"+ - "\5\2\2\2\17\20\7\4\2\2\20\24\3\2\2\2\21\22\7\5\2\2\22\24\5\2\2\4\23\6"+ - "\3\2\2\2\23\f\3\2\2\2\23\r\3\2\2\2\23\21\3\2\2\2\24\32\3\2\2\2\25\26\f"+ - "\3\2\2\26\27\7\5\2\2\27\31\5\2\2\4\30\25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3\2\2\2\60.\3\2\2\2\60"+ + "\61\3\2\2\2\61\7\3\2\2\2\62\60\3\2\2\2\63\66\7\b\2\2\64\66\7\n\2\2\65"+ + "\63\3\2\2\2\65\64\3\2\2\2\66\t\3\2\2\2\7\16\32)\60\65"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefVisitor.java b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefVisitor.java index 37b4db562..a51c29b20 100644 --- a/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefVisitor.java +++ b/src/test/java/dk/camelot64/kickc/parsing/typedef/TypedefVisitor.java @@ -1,5 +1,9 @@ // Generated from /Users/jespergravgaard/c64/kickc/src/test/java/dk/camelot64/kickc/parsing/typedef/Typedef.g4 by ANTLR 4.7 package dk.camelot64.kickc.parsing.typedef; + + import java.util.ArrayList; + import java.util.List; + import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** @@ -10,6 +14,26 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor; * operations with no return type. */ public interface TypedefVisitor extends ParseTreeVisitor { + /** + * Visit a parse tree produced by {@link TypedefParser#stmtSeq}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStmtSeq(TypedefParser.StmtSeqContext ctx); + /** + * Visit a parse tree produced by the {@code stmtTypeDef} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStmtTypeDef(TypedefParser.StmtTypeDefContext ctx); + /** + * Visit a parse tree produced by the {@code stmtExpr} + * labeled alternative in {@link TypedefParser#stmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStmtExpr(TypedefParser.StmtExprContext ctx); /** * Visit a parse tree produced by the {@code exprValueName} * labeled alternative in {@link TypedefParser#expr}.