diff --git a/grammar/MetaCode.g4 b/grammar/MetaCode.g4 new file mode 100644 index 0000000..9ef2a0a --- /dev/null +++ b/grammar/MetaCode.g4 @@ -0,0 +1,23 @@ +grammar MetaCode; + +init : (NUMBER NEWLINE?)+ + ; + +WHITESPACE : [ \t]+ -> skip; +NEWLINE : '\r'? '\n' -> skip; + +NUMBER : INT + | FLOAT; + +fragment +INT : DIGIT+ + ; + +fragment +FLOAT : DIGIT+ '.' DIGIT* + | '.' DIGIT+ + ; + +fragment +DIGIT : [0-9] + ; \ No newline at end of file diff --git a/grammar/example.txt b/grammar/example.txt new file mode 100644 index 0000000..0b4445e --- /dev/null +++ b/grammar/example.txt @@ -0,0 +1,2 @@ +545 +18. \ No newline at end of file diff --git a/grammar/run.rb b/grammar/run.rb new file mode 100644 index 0000000..bed61a5 --- /dev/null +++ b/grammar/run.rb @@ -0,0 +1,11 @@ + example = if ARGV[0].nil? + File.open("example.txt", "w") if not File.exist?("example.txt") + "example.txt" + else + ARGV[0].to_s + end + puts "Running MetaCode parser ..." + Dir.mkdir("src") if not Dir.exists? "src" + Dir.chdir("src") + system('call antlr4 "../MetaCode.g4" -visitor') + system('call grun MetaCode init -gui ..\\' + example) diff --git a/grammar/src/MetaCode.tokens b/grammar/src/MetaCode.tokens new file mode 100644 index 0000000..4497d1d --- /dev/null +++ b/grammar/src/MetaCode.tokens @@ -0,0 +1,3 @@ +WHITESPACE=1 +NEWLINE=2 +NUMBER=3 diff --git a/grammar/src/MetaCodeBaseListener.class b/grammar/src/MetaCodeBaseListener.class new file mode 100644 index 0000000..a9f99fa Binary files /dev/null and b/grammar/src/MetaCodeBaseListener.class differ diff --git a/grammar/src/MetaCodeBaseListener.java b/grammar/src/MetaCodeBaseListener.java new file mode 100644 index 0000000..5247b0d --- /dev/null +++ b/grammar/src/MetaCodeBaseListener.java @@ -0,0 +1,51 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link MetaCodeListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class MetaCodeBaseListener implements MetaCodeListener { + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void enterInit(@NotNull MetaCodeParser.InitContext ctx) { } + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void exitInit(@NotNull MetaCodeParser.InitContext ctx) { } + + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void enterEveryRule(@NotNull ParserRuleContext ctx) { } + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void exitEveryRule(@NotNull ParserRuleContext ctx) { } + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void visitTerminal(@NotNull TerminalNode node) { } + /** + * {@inheritDoc} + *

+ * The default implementation does nothing. + */ + @Override public void visitErrorNode(@NotNull ErrorNode node) { } +} \ No newline at end of file diff --git a/grammar/src/MetaCodeBaseVisitor.class b/grammar/src/MetaCodeBaseVisitor.class new file mode 100644 index 0000000..74aa90e Binary files /dev/null and b/grammar/src/MetaCodeBaseVisitor.class differ diff --git a/grammar/src/MetaCodeBaseVisitor.java b/grammar/src/MetaCodeBaseVisitor.java new file mode 100644 index 0000000..43031e9 --- /dev/null +++ b/grammar/src/MetaCodeBaseVisitor.java @@ -0,0 +1,21 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + +/** + * This class provides an empty implementation of {@link MetaCodeVisitor}, + * which can be extended to create a visitor which only needs to handle a subset + * of the available methods. + * + * @param The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public class MetaCodeBaseVisitor extends AbstractParseTreeVisitor implements MetaCodeVisitor { + /** + * {@inheritDoc} + *

+ * The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}. + */ + @Override public T visitInit(@NotNull MetaCodeParser.InitContext ctx) { return visitChildren(ctx); } +} \ No newline at end of file diff --git a/grammar/src/MetaCodeLexer.class b/grammar/src/MetaCodeLexer.class new file mode 100644 index 0000000..af7bfec Binary files /dev/null and b/grammar/src/MetaCodeLexer.class differ diff --git a/grammar/src/MetaCodeLexer.java b/grammar/src/MetaCodeLexer.java new file mode 100644 index 0000000..89f5da5 --- /dev/null +++ b/grammar/src/MetaCodeLexer.java @@ -0,0 +1,96 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class MetaCodeLexer extends Lexer { + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + WHITESPACE=1, NEWLINE=2, NUMBER=3; + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + public static final String[] tokenNames = { + "", + "WHITESPACE", "NEWLINE", "NUMBER" + }; + public static final String[] ruleNames = { + "WHITESPACE", "NEWLINE", "NUMBER", "INT", "FLOAT", "DIGIT" + }; + + + public MetaCodeLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "MetaCode.g4"; } + + @Override + public String[] getTokenNames() { return tokenNames; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + @Override + public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { + switch (ruleIndex) { + case 0: WHITESPACE_action((RuleContext)_localctx, actionIndex); break; + + case 1: NEWLINE_action((RuleContext)_localctx, actionIndex); break; + } + } + private void WHITESPACE_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 0: skip(); break; + } + } + private void NEWLINE_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 1: skip(); break; + } + } + + public static final String _serializedATN = + "\3\uacf5\uee8c\u4f5d\u8b0d\u4a45\u78bd\u1b2f\u3378\2\5<\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\3\2\6\2\21\n\2\r\2\16\2\22\3\2"+ + "\3\2\3\3\5\3\30\n\3\3\3\3\3\3\3\3\3\3\4\3\4\5\4 \n\4\3\5\6\5#\n\5\r\5"+ + "\16\5$\3\6\6\6(\n\6\r\6\16\6)\3\6\3\6\7\6.\n\6\f\6\16\6\61\13\6\3\6\3"+ + "\6\6\6\65\n\6\r\6\16\6\66\5\69\n\6\3\7\3\7\2\b\3\3\2\5\4\3\7\5\1\t\2\1"+ + "\13\2\1\r\2\1\3\2\4\4\2\13\13\"\"\3\2\62;@\2\3\3\2\2\2\2\5\3\2\2\2\2\7"+ + "\3\2\2\2\3\20\3\2\2\2\5\27\3\2\2\2\7\37\3\2\2\2\t\"\3\2\2\2\138\3\2\2"+ + "\2\r:\3\2\2\2\17\21\t\2\2\2\20\17\3\2\2\2\21\22\3\2\2\2\22\20\3\2\2\2"+ + "\22\23\3\2\2\2\23\24\3\2\2\2\24\25\b\2\2\2\25\4\3\2\2\2\26\30\7\17\2\2"+ + "\27\26\3\2\2\2\27\30\3\2\2\2\30\31\3\2\2\2\31\32\7\f\2\2\32\33\3\2\2\2"+ + "\33\34\b\3\3\2\34\6\3\2\2\2\35 \5\t\5\2\36 \5\13\6\2\37\35\3\2\2\2\37"+ + "\36\3\2\2\2 \b\3\2\2\2!#\5\r\7\2\"!\3\2\2\2#$\3\2\2\2$\"\3\2\2\2$%\3\2"+ + "\2\2%\n\3\2\2\2&(\5\r\7\2\'&\3\2\2\2()\3\2\2\2)\'\3\2\2\2)*\3\2\2\2*+"+ + "\3\2\2\2+/\7\60\2\2,.\5\r\7\2-,\3\2\2\2.\61\3\2\2\2/-\3\2\2\2/\60\3\2"+ + "\2\2\609\3\2\2\2\61/\3\2\2\2\62\64\7\60\2\2\63\65\5\r\7\2\64\63\3\2\2"+ + "\2\65\66\3\2\2\2\66\64\3\2\2\2\66\67\3\2\2\2\679\3\2\2\28\'\3\2\2\28\62"+ + "\3\2\2\29\f\3\2\2\2:;\t\3\2\2;\16\3\2\2\2\13\2\22\27\37$)/\668"; + public static final ATN _ATN = + ATNSimulator.deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/grammar/src/MetaCodeLexer.tokens b/grammar/src/MetaCodeLexer.tokens new file mode 100644 index 0000000..4497d1d --- /dev/null +++ b/grammar/src/MetaCodeLexer.tokens @@ -0,0 +1,3 @@ +WHITESPACE=1 +NEWLINE=2 +NUMBER=3 diff --git a/grammar/src/MetaCodeListener.class b/grammar/src/MetaCodeListener.class new file mode 100644 index 0000000..22ae0c2 Binary files /dev/null and b/grammar/src/MetaCodeListener.class differ diff --git a/grammar/src/MetaCodeListener.java b/grammar/src/MetaCodeListener.java new file mode 100644 index 0000000..ff558da --- /dev/null +++ b/grammar/src/MetaCodeListener.java @@ -0,0 +1,20 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link MetaCodeParser}. + */ +public interface MetaCodeListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link MetaCodeParser#init}. + * @param ctx the parse tree + */ + void enterInit(@NotNull MetaCodeParser.InitContext ctx); + /** + * Exit a parse tree produced by {@link MetaCodeParser#init}. + * @param ctx the parse tree + */ + void exitInit(@NotNull MetaCodeParser.InitContext ctx); +} \ No newline at end of file diff --git a/grammar/src/MetaCodeParser$InitContext.class b/grammar/src/MetaCodeParser$InitContext.class new file mode 100644 index 0000000..d9f9559 Binary files /dev/null and b/grammar/src/MetaCodeParser$InitContext.class differ diff --git a/grammar/src/MetaCodeParser.class b/grammar/src/MetaCodeParser.class new file mode 100644 index 0000000..5bea99e Binary files /dev/null and b/grammar/src/MetaCodeParser.class differ diff --git a/grammar/src/MetaCodeParser.java b/grammar/src/MetaCodeParser.java new file mode 100644 index 0000000..9b1a0f6 --- /dev/null +++ b/grammar/src/MetaCodeParser.java @@ -0,0 +1,125 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class MetaCodeParser extends Parser { + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + WHITESPACE=1, NEWLINE=2, NUMBER=3; + public static final String[] tokenNames = { + "", "WHITESPACE", "NEWLINE", "NUMBER" + }; + public static final int + RULE_init = 0; + public static final String[] ruleNames = { + "init" + }; + + @Override + public String getGrammarFileName() { return "MetaCode.g4"; } + + @Override + public String[] getTokenNames() { return tokenNames; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public ATN getATN() { return _ATN; } + + public MetaCodeParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + public static class InitContext extends ParserRuleContext { + public List NEWLINE() { return getTokens(MetaCodeParser.NEWLINE); } + public TerminalNode NEWLINE(int i) { + return getToken(MetaCodeParser.NEWLINE, i); + } + public TerminalNode NUMBER(int i) { + return getToken(MetaCodeParser.NUMBER, i); + } + public List NUMBER() { return getTokens(MetaCodeParser.NUMBER); } + public InitContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_init; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof MetaCodeListener ) ((MetaCodeListener)listener).enterInit(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof MetaCodeListener ) ((MetaCodeListener)listener).exitInit(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof MetaCodeVisitor ) return ((MetaCodeVisitor)visitor).visitInit(this); + else return visitor.visitChildren(this); + } + } + + public final InitContext init() throws RecognitionException { + InitContext _localctx = new InitContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_init); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(6); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(2); match(NUMBER); + setState(4); + _la = _input.LA(1); + if (_la==NEWLINE) { + { + setState(3); match(NEWLINE); + } + } + + } + } + setState(8); + _errHandler.sync(this); + _la = _input.LA(1); + } while ( _la==NUMBER ); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\uacf5\uee8c\u4f5d\u8b0d\u4a45\u78bd\u1b2f\u3378\3\5\r\4\2\t\2\3\2\3"+ + "\2\5\2\7\n\2\6\2\t\n\2\r\2\16\2\n\3\2\2\3\2\2\2\r\2\b\3\2\2\2\4\6\7\5"+ + "\2\2\5\7\7\4\2\2\6\5\3\2\2\2\6\7\3\2\2\2\7\t\3\2\2\2\b\4\3\2\2\2\t\n\3"+ + "\2\2\2\n\b\3\2\2\2\n\13\3\2\2\2\13\3\3\2\2\2\4\6\n"; + public static final ATN _ATN = + ATNSimulator.deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/grammar/src/MetaCodeVisitor.class b/grammar/src/MetaCodeVisitor.class new file mode 100644 index 0000000..fbe4d0b Binary files /dev/null and b/grammar/src/MetaCodeVisitor.class differ diff --git a/grammar/src/MetaCodeVisitor.java b/grammar/src/MetaCodeVisitor.java new file mode 100644 index 0000000..5cb13da --- /dev/null +++ b/grammar/src/MetaCodeVisitor.java @@ -0,0 +1,19 @@ +// Generated from ../MetaCode.g4 by ANTLR 4.1 +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.tree.ParseTreeVisitor; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link MetaCodeParser}. + * + * @param The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public interface MetaCodeVisitor extends ParseTreeVisitor { + /** + * Visit a parse tree produced by {@link MetaCodeParser#init}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitInit(@NotNull MetaCodeParser.InitContext ctx); +} \ No newline at end of file diff --git a/scripts/antlr4.rb b/scripts/antlr4.rb new file mode 100644 index 0000000..7a1279d --- /dev/null +++ b/scripts/antlr4.rb @@ -0,0 +1,39 @@ +switches = [] + +ARGV.each do |arg| + switches << arg if arg.start_with? "-" +end + +if switches.include? "-init" + if not ARGV[1].nil? + File.open("run.rb", "w") { |file| + puts "Initialize grammar ..." + code = <<-CODE + example = if ARGV[0].nil? + File.open("example.txt", "w") if not File.exist?("example.txt") + "example.txt" + else + ARGV[0].to_s + end + puts "Running #{ARGV[1]} parser ..." + Dir.mkdir("src") if not Dir.exists? "src" + Dir.chdir("src") + system('call antlr4 "../#{ARGV[1]}.g4" -visitor') + system('call grun #{ARGV[1]} init -gui ..\\\\' + example) + CODE + file.puts(code) + puts "Initialization is completed." + } + else + puts "Use this: antlr4 -init " + end +elsif switches.include? "-help" + puts "Usage: 'antlr4 ' or" + puts " 'antlr4 '" + puts "Switches: " + puts " -init Initialize ANTLR grammar" + puts " -help Show help" +else + system("java -cp e:\Development\Libraries\ANTLR\antlr-4.1-complete.jar;%CLASSPATH% org.antlr.v4.Tool #{ARGV[0]} -visitor") + system("javac *.java") +end \ No newline at end of file diff --git a/scripts/grun.bat b/scripts/grun.bat new file mode 100644 index 0000000..ebcb057 --- /dev/null +++ b/scripts/grun.bat @@ -0,0 +1 @@ +java -cp .;%CLASSPATH% org.antlr.v4.runtime.misc.TestRig %* \ No newline at end of file