Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer.
Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer. Can also pass in a string or char[] to use. If you need encoding, pass in stream/reader with correct encoding.
Inheritance: ICharStream
        public Questionnaire BuildAST(string inputString)
        {
            MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? ""));

            AntlrInputStream antlrInputStream = new AntlrInputStream(inputStream);
            QLLexer lexer = new QLLexer(antlrInputStream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            _parser = new QLParser(tokens);

            //Replaxe lexer/parser error listeners
            lexer.RemoveErrorListeners();
            _parser.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener() { OnError = LexerErrors.Add });
            _parser.AddErrorListener(new ParserErrorListener() { OnError = ParserErrors.Add });

            //set manager on partial parser class
            _parser.SetIdManager(Memory);

            //build AST
            _parseTree = _parser.questionnaire();
            AST = _parser.GetAST();

            //check for lexer/parser errors
            if (!LexerErrors.Any() && !ParserErrors.Any())
            {
                TypeChecker.Run(AST);
            }

            return AST;
        }
Exemple #2
0
        public static App ReadAppText(string inputString)
        {
            var input = new Antlr4.Runtime.AntlrInputStream(inputString);
            var app   = GestureAppFromAntlrInput(input);

            return(app);
        }
        public static bool TryGetLineStatements(string text, int lineNumber, out IList<IParseTree> statementTrees, out IList<IToken> tokens)
        {
            Contract.Requires<ArgumentNullException>(text != null, "text");
            Contract.Requires<ArgumentOutOfRangeException>(lineNumber >= 0);

            try
            {
                AntlrInputStream input = new AntlrInputStream(text);
                JavaLexer lexer = new JavaLexer(new JavaUnicodeStreamV4(input));
                CommonTokenStream tokenStream = new CommonTokenStream(lexer);
                JavaParser parser = new JavaParser(tokenStream);

                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                parser.BuildParseTree = true;
                JavaParser.CompilationUnitContext result = parser.compilationUnit();

                statementTrees = null;
                tokens = tokenStream.GetTokens();

                AssociatedTreeListener listener = new AssociatedTreeListener(lineNumber, tokens);
                ParseTreeWalker.Default.Walk(listener, result);
                statementTrees = listener.StatementTrees;

                return true;
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                    throw;

                statementTrees = null;
                tokens = null;
                return false;
            }
        }
        /// <summary>
        /// Exports the specified component to a temporary file, loads, and then parses the exported file.
        /// </summary>
        /// <param name="component"></param>
        public IDictionary<Tuple<string, DeclarationType>, Attributes> Parse(VBComponent component)
        {
            var path = _exporter.Export(component);
            if (!File.Exists(path))
            {
                // a document component without any code wouldn't be exported (file would be empty anyway).
                return new Dictionary<Tuple<string, DeclarationType>, Attributes>();
            }

            var code = File.ReadAllText(path);
            File.Delete(path);

            var type = component.Type == vbext_ComponentType.vbext_ct_StdModule
                ? DeclarationType.Module
                : DeclarationType.Class;
            var listener = new AttributeListener(Tuple.Create(component.Name, type));

            var stream = new AntlrInputStream(code);
            var lexer = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            // parse tree isn't usable for declarations because
            // line numbers are offset due to module header and attributes
            // (these don't show up in the VBE, that's why we're parsing an exported file)
            var tree = parser.startRule();
            ParseTreeWalker.Default.Walk(listener, tree);

            return listener.Attributes;
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            var path = @"C:\Users\alexander.higgins\source\repos\ILDisassembler\MSILParser\IlDisassembler.il";
            var text = File.ReadAllText(path);

            var stream = new Antlr4.Runtime.AntlrInputStream(text);
            var lexer  = new MSILLexer((ICharStream)stream);

            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new MSILParser(tokenStream);

            parser.BuildParseTree = true;

            var ctx = parser.decls();

            MSILParser.StartContext start = parser.start();

            var classDecs = parser.start();

            var vistor = new MsilVisitor();

            vistor.Visit(start);
            var decl = parser.decl();

            vistor.Visit(decl);
            //parser.d
        }
Exemple #6
0
 protected void Build(string input)
 {
     Inputstream = new AntlrInputStream(input);
     Lexer = new QLLexer(Inputstream);
     Tokenstream = new CommonTokenStream(Lexer);
     Parser = new QLParser(Tokenstream);
 }
        public void ParsesEmptyForm()
        {
            var code = @"
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} Form1 
   Caption         =   ""Form1""
   ClientHeight    =   2640
   ClientLeft      =   45
   ClientTop       =   375
   ClientWidth     =   4710
   OleObjectBlob   =   ""Form1.frx"":0000
   StartUpPosition =   1  'CenterOwner
End
Attribute VB_Name = ""Form1""
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
";
            var stream = new AntlrInputStream(code);
            var lexer = new VBALexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);
            parser.ErrorListeners.Clear();
            parser.ErrorListeners.Add(new ExceptionErrorListener());
            var tree = parser.startRule();
            Assert.IsNotNull(tree);
        }
 public Expression(string expression)
 {
     var input = new AntlrInputStream(expression);
     var lexer = new EstimatingExpressionEvaluatorLexer(input);
     var tokens = new CommonTokenStream(lexer);
     this.parser = new EstimatingExpressionEvaluatorParser(tokens);
     this.parseTree = parser.parse();
 }
 void IScanner.SetSource(string source, int offset)
 {
     m_source = source.Substring(offset);
     var inputStream = new AntlrInputStream(m_source);
     var lexer = new cqlLexer(inputStream);
     m_tokens = lexer.GetAllTokens();
     m_tokenIndex = 0;
 }
 public static void Main(string[] args)
 {
     Stream inputStream = Console.OpenStandardInput();
     AntlrInputStream input = new AntlrInputStream(inputStream);
     CalculatorLexer lexer = new CalculatorLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     CalculatorParser parser = new CalculatorParser(tokens);
 }
Exemple #11
0
 private void Init()
 {
     var inputStream = new AntlrInputStream(new System.IO.StreamReader(_filename));
     var lexer = new PropositionalCalculusLexer(inputStream);
     var tokenStream = new CommonTokenStream(lexer);
     _parser = new PropositionalCalculusParser(tokenStream) { BuildParseTree = true };
     _tree = _parser.text();
 }
        public String GetParseTreeAsString(String rule)
        {
            AntlrInputStream input = new AntlrInputStream(rule);
            RuleSetLexer lexer = new RuleSetLexer(input);
            var tokens = new CommonTokenStream(lexer);
            RuleSetParser parser = new RuleSetParser(tokens);

            return parser.rule_set().ToStringTree(parser);
        }
 public void ParseClojure() {
     var inputStream = new AntlrInputStream("(def lst '(a b c))");
     var lexer = new ClojureLexer(inputStream);
     var commonTokenStream = new CommonTokenStream(lexer);
     var parser = new ClojureParser(commonTokenStream);
     var visitor = new CstBuilderForAntlr4(parser);
     visitor.Visit(parser.file());
     Console.WriteLine(visitor.FinishParsing());
 }
        public static List<Expression> getConstraints()
        {
            List<Expression> pathConstraints = new List<Expression>();

            FileStream fileStream = new FileStream(@"c:\example2.txt", FileMode.Open, FileAccess.Read);
            AntlrInputStream input = new AntlrInputStream(fileStream);
            VBGrammarLexer lexer = new VBGrammarLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            VBGrammarParser parser = new VBGrammarParser(tokens);
            IParseTree tree = parser.startRule();
            //Console.WriteLine(tree.ToStringTree(parser));

            // Use visitor pattern to get "raw" constraints
            VbaTreeVisitor eval = new VbaTreeVisitor();
            Expression exp = eval.Visit(tree);
            List<RawConstraint> rawConstraints = eval.rawConstraints;

            // Create negated expressions for ELSE blocks
            RawConstraint.updateElseBlocks(rawConstraints);

            // Update constraints to transform into binary tree and get leaf nodes
            RawConstraint.updateParents(rawConstraints);
            List<RawConstraint> leafNodes = RawConstraint.getLeafNodes(rawConstraints);

            // For each leaf node, work the way up to generate PathConstraints
            foreach (RawConstraint leaf in leafNodes)
            {
                var parentLine = leaf.ParentLineNumber;
                if (leaf.Expr == null) { continue; }

                // find parent
                RawConstraint parent = findParent(rawConstraints, parentLine);

                // AND with parent
                BinaryExpression binExp = (BinaryExpression)leaf.Expr;
                while (parent != null)
                {
                    if (parent.Expr == null)
                    {
                        break;
                    }
                    else
                    {
                        binExp = BinaryExpression.And(binExp, parent.Expr);
                    }

                    parent = findParent(rawConstraints, parent.ParentLineNumber);
                }

                pathConstraints.Add(binExp);
                // Console.WriteLine("Leaf: " + leaf.Expr);
                // Console.WriteLine("AND: " + binExp);
            }

            return pathConstraints;
        }
Exemple #15
0
 public static void Main()
 {
     var s = new Antlr4.Runtime.AntlrInputStream(File.OpenRead("Program.m"));
     var l = new MethLab.Parser.ANTLR.MATLABLexer(s);
     var p = new MethLab.Parser.ANTLR.MATLABParser(new CommonTokenStream(l));
     var o = new MatlabListener();
     var w = new ParseTreeWalker();
     w.Walk(o, p.file());
     Console.ReadLine();
 }
Exemple #16
0
        static void Main(string[] args)
        {
            var str    = new Antlr4.Runtime.AntlrInputStream("1 + 2");
            var lexer  = new arithmeticLexer(str);
            var tokens = new CommonTokenStream(lexer);
            var parser = new arithmeticParser(tokens);
            var tree   = parser.expression();

            System.Console.WriteLine(tree.ToStringTree(parser.RuleNames));
        }
 public IEnumerable<NodeSelector> Parse(string source)
 {
     var inputStream = new AntlrInputStream(source);
     var lexer = new TreeSelectorLexer(inputStream);
     var commonTokenStream = new CommonTokenStream(lexer);
     var parser = new TreeSelectorParser(commonTokenStream);
     var context = parser.init();
     var visitor = new TreeSelectorVisitor();
     return visitor.VisitInit(context);
 }
Exemple #18
0
 /// <summary>
 /// Using http://programming-pages.com/2013/12/14/antlr-4-with-c-and-visual-studio-2012/ as a guide.
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
     string test = "a = 10*10";
     var input = new AntlrInputStream(test);
     var lexer = new calculatorLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     var parser = new calculatorParser(tokens);
     var tree = parser.equation();
     Console.WriteLine(tree.ToStringTree(parser));
 }
 public Library TranslateLibrary(string source)
 {
     var inputStream = new AntlrInputStream(source);
     var lexer = new cqlLexer(inputStream);
     var tokenStream = new CommonTokenStream(lexer);
     var parser = new cqlParser(tokenStream);
     var parseTree = parser.logic();
     var visitor = new cqlTranslationVisitor();
     return visitor.Visit(parseTree) as Library;
 }
Exemple #20
0
 /// <summary>
 /// Parses the specified input.
 /// </summary>
 /// <param name="input">The input.</param>
 /// <returns>The AST for the input</returns>
 /// <exception cref="System.ArgumentNullException">input</exception>
 public INode Parse(string input)
 {
     if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input");
     AntlrInputStream InputStream = new AntlrInputStream(input);
     PNZRLexer Lexer = new PNZRLexer(InputStream);
     CommonTokenStream TokenStream = new CommonTokenStream(Lexer);
     PNZRParser Parser = new PNZRParser(TokenStream);
     IParseTree Tree = Parser.program();
     PNZRVisitor Visitor = new PNZRVisitor();
     return Visitor.Visit(Tree);
 }
Exemple #21
0
        public InterfaceExtruder(string objcFileContent)
        {
            content = objcFileContent;
            var input = new AntlrInputStream (content);
            lexer = new ObjCLexer (input);
            tokens = new CommonTokenStream (lexer);
            parser = new ObjCParser (tokens);

            listner = new ObjCListnerImpl ();
            walker = new ParseTreeWalker ();
        }
Exemple #22
0
        static void Main(string[] args)
        {
            Stream inputStream = Console.OpenStandardInput();
            AntlrInputStream input = new AntlrInputStream(inputStream);
            CalculatorLexer lexer = new CalculatorLexer(input);
            lexer.AddErrorHandlers();
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CalculatorParser parser = new CalculatorParser(tokens);
            parser.addSubExpr();

            Console.ReadLine();
        }
 public Document Build(string data)
 {
     var stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
     var reader = new StreamReader(stream);
     var input = new AntlrInputStream(reader);
     var lexer = new GraphQLLexer(input);
     var tokens = new CommonTokenStream(lexer);
     var parser = new GraphQLParser(tokens);
     var documentTree = parser.document();
     var vistor = new GraphQLVisitor();
     return vistor.Visit(documentTree) as Document;
 }
Exemple #24
0
        static void CreateCode()
        {
            AntlrInputStream input = new AntlrInputStream(File.OpenRead(structurePath));

            eRegsGrammarLexer lexer = new eRegsGrammarLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            eRegsGrammarParser parser = new eRegsGrammarParser(tokens);

            IParseTree tree = parser.r();
            MyVisitor visitor = new MyVisitor();
            File.WriteAllText(classPath, visitor.Visit(tree).ToString());
        }
Exemple #25
0
 public void TestGrammar()
 {
     String input = "hello zome\r\nhello tezt";
     AntlrInputStream stream = new AntlrInputStream(input);
     ITokenSource lexer = new TagzLexer(stream);
     ITokenStream tokens = new CommonTokenStream(lexer);
     TagzParser parser = new TagzParser(tokens);
     parser.BuildParseTree = true;
     IParseTree tree = parser.r();
     TagzKeyPrinter printer = new TagzKeyPrinter();
     ParseTreeWalker.Default.Walk(printer, tree);
 }
 public static LinearIntegerExpression parseLinearExpression(String input)
 {
     var stream = new AntlrInputStream(input);
     var lexer = new SymbolicStringsLexer(stream);
     var tokens = new CommonTokenStream(lexer);
     var parser = new SymbolicStringsParser(tokens);
     parser.BuildParseTree = true;
     var tree = parser.integer();
     if (parser.NumberOfSyntaxErrors == 0)
         return tree.value;
     return null;
 }
Exemple #27
0
        public static App ReadAppText(string inputString)
        {
            //var input = new Antlr4.Runtime.AntlrFileStream(filename);    //"..\\..\\Tests\\simple.app"
            var input   = new Antlr4.Runtime.AntlrInputStream(inputString);              //"..\\..\\Tests\\simple.app"
            var lexer   = new PreposeGesturesLexer(input);
            var tokens  = new CommonTokenStream(lexer);
            var parser  = new PreposeGesturesParser(tokens);
            var tree    = parser.app();          // parse
            var visitor = new AppConverter();
            var app     = (App)visitor.Visit(tree);

            return(app);
        }
 public static SymbolicString parseSymbolicString(String input, List<String> alphabet)
 {
     var stream = new AntlrInputStream(input);
     var lexer = new SymbolicStringsLexer(stream);
     var tokens = new CommonTokenStream(lexer);
     var parser = new SymbolicStringsParser(tokens);
     parser.alphabet = alphabet;
     parser.BuildParseTree = true;
     var tree = parser.symbolic_string();
     if (parser.NumberOfSyntaxErrors == 0 && parser.word_error == null)
         return tree.value;
     return null;
 }
        public void ParseC() {
            var inputStream = new AntlrInputStream(@"
int main(int argc, char **args) {
	printf(args[0]);
	return 0;
}");
            var lexer = new CLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(lexer);
            var parser = new CParser(commonTokenStream);
            var visitor = new CstBuilderForAntlr4(parser);
            visitor.Visit(parser.compilationUnit());
            Console.WriteLine(visitor.FinishParsing());
        }
Exemple #30
0
        public static App ReadAppText(string inputString)
        {
            //var input = new Antlr4.Runtime.AntlrFileStream(filename);    //"..\\..\\Tests\\simple.app"
            var input = new Antlr4.Runtime.AntlrInputStream(inputString);    //"..\\..\\Tests\\simple.app"
            var lexer = new PreposeGesturesLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new PreposeGesturesParser(tokens);
            var tree = parser.app(); // parse
            var visitor = new AppConverter();
            var app = (App)visitor.Visit(tree);

            return app;
        }
Exemple #31
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Error: No arguments given");
                Console.WriteLine("Usage: AdaGrammar.exe <filename>");
                return;
            }

            StreamReader stream = null;

            try
            {
                stream = new StreamReader(args[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
                return;
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            using (stream)
            {
                try
                {
                    var               input  = new Antlr4.Runtime.AntlrInputStream(stream);
                    AdaLexer          lexer  = new AdaLexer(input);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    AdaParser         parser = new AdaParser(tokens);

                    var result = parser.compilation_unit();
                }
                finally
                {
                    stream.Close();
                }
            }

            stopwatch.Stop();
            Console.WriteLine("Parse Elapsed time: {0}", stopwatch.Elapsed);
        }
Exemple #32
0
        ///<summary>
        /// Constructor.
        ///</summary>
        public DomainProblem(string domainfile, string problemfile) : this()
        {
            using (var domstream = new StreamReader(domainfile)) {
                var inp    = new Antlr4.Runtime.AntlrInputStream(domstream);
                var lexer  = new pddlLexer(inp);
                var stream = new Antlr4.Runtime.CommonTokenStream(lexer);
                var parser = new pddlParser(stream);
                var tree   = parser.domain();
                this.domain = new DomainListener();
                var walker = new Antlr4.Runtime.Tree.ParseTreeWalker();
                walker.Walk(this.domain, tree);
            }

            using (var probstream = new StreamReader(problemfile)) {
                var inp    = new Antlr4.Runtime.AntlrInputStream(probstream);
                var lexer  = new pddlLexer(inp);
                var stream = new Antlr4.Runtime.CommonTokenStream(lexer);
                var parser = new pddlParser(stream);
                var tree   = parser.problem();
                this.problem = new ProblemListener();
                var walker = new Antlr4.Runtime.Tree.ParseTreeWalker();
                walker.Walk(this.problem, tree);
            }
        }
Exemple #33
0
    static void Main(string[] args)
    {
        bool   show_tree   = false;
        bool   show_tokens = false;
        bool   old         = false;
        bool   two_byte    = false;
        string file_name   = null;
        string input       = null;

        System.Text.Encoding encoding = null;
        for (int i = 0; i < args.Length; ++i)
        {
            if (args[i].Equals("-tokens"))
            {
                show_tokens = true;
                continue;
            }
            else if (args[i].Equals("-two-byte"))
            {
                two_byte = true;
                continue;
            }
            else if (args[i].Equals("-old"))
            {
                old = true;
                continue;
            }
            else if (args[i].Equals("-tree"))
            {
                show_tree = true;
                continue;
            }
            else if (args[i].Equals("-input"))
            {
                input = args[++i];
            }
            else if (args[i].Equals("-file"))
            {
                file_name = args[++i];
            }
            else if (args[i].Equals("-encoding"))
            {
                ++i;
                encoding = Encoding.GetEncoding(
                    args[i],
                    new EncoderReplacementFallback("(unknown)"),
                    new DecoderReplacementFallback("(error)"));
                if (encoding == null)
                {
                    throw new Exception(@"Unknown encoding. Must be an Internet Assigned Numbers Authority (IANA) code page name. https://www.iana.org/assignments/character-sets/character-sets.xhtml");
                }
            }
        }
        ICharStream str = null;

        if (input == null && file_name == null)
        {
            str = CharStreams.fromStream(System.Console.OpenStandardInput());
        }
        else if (input != null)
        {
            str = CharStreams.fromString(input);
        }
        else if (file_name != null)
        {
            if (two_byte)
            {
                str = new TwoByteCharStream(file_name);
            }
            else if (old)
            {
                FileStream fs = new FileStream(file_name, FileMode.Open);
                str = new Antlr4.Runtime.AntlrInputStream(fs);
            }
            else if (encoding == null)
            {
                str = CharStreams.fromPath(file_name);
            }
            else
            {
                str = CharStreams.fromPath(file_name, encoding);
            }
        }
        var lexer = new JavaScriptLexer(str);

        if (show_tokens)
        {
            StringBuilder new_s = new StringBuilder();
            for (int i = 0; ; ++i)
            {
                var ro_token = lexer.NextToken();
                var token    = (CommonToken)ro_token;
                token.TokenIndex = i;
                new_s.AppendLine(token.ToString());
                if (token.Type == Antlr4.Runtime.TokenConstants.EOF)
                {
                    break;
                }
            }
            System.Console.Error.WriteLine(new_s.ToString());
            lexer.Reset();
        }
        var tokens          = new CommonTokenStream(lexer);
        var parser          = new JavaScriptParser(tokens);
        var listener_lexer  = new ErrorListener <int>();
        var listener_parser = new ErrorListener <IToken>();

        lexer.AddErrorListener(listener_lexer);
        parser.AddErrorListener(listener_parser);
        DateTime before = DateTime.Now;
        var      tree   = parser.program();
        DateTime after  = DateTime.Now;

        System.Console.Error.WriteLine("Time: " + (after - before));
        if (listener_lexer.had_error || listener_parser.had_error)
        {
            System.Console.Error.WriteLine("Parse failed.");
        }
        else
        {
            System.Console.Error.WriteLine("Parse succeeded.");
        }
        if (show_tree)
        {
            System.Console.Error.WriteLine(tree.ToStringTree(parser));
        }
        System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0);
    }
Exemple #34
0
    static void Main(string[] args)
    {
        bool   show_tree   = false;
        bool   show_tokens = false;
        bool   old         = false;
        bool   two_byte    = false;
        string file_name   = null;
        string input       = null;

        System.Text.Encoding encoding = null;
        for (int i = 0; i \ < args.Length; ++i)
        {
            if (args[i].Equals("-tokens"))
            {
                show_tokens = true;
                continue;
            }
            else if (args[i].Equals("-two-byte"))
            {
                two_byte = true;
                continue;
            }
            else if (args[i].Equals("-old"))
            {
                old = true;
                continue;
            }
            else if (args[i].Equals("-tree"))
            {
                show_tree = true;
                continue;
            }
            else if (args[i].Equals("-input"))
            {
                input = args[++i];
            }
            else if (args[i].Equals("-file"))
            {
                file_name = args[++i];
            }
            else if (args[i].Equals("-encoding"))
            {
                ++i;
                encoding = Encoding.GetEncoding(
                    args[i],
                    new EncoderReplacementFallback("(unknown)"),
                    new DecoderReplacementFallback("(error)"));
                if (encoding == null)
                {
                    throw new Exception(@"Unknown encoding. Must be an Internet Assigned Numbers Authority (IANA) code page name. https://www.iana.org/assignments/character-sets/character-sets.xhtml");
                }
            }
        }
        ICharStream str = null;

        if (input == null && file_name == null)
        {
            str = CharStreams.fromStream(System.Console.OpenStandardInput());
        }
        else if (input != null)
        {
            str = CharStreams.fromString(input);
        }
        else if (file_name != null)
        {
            if (two_byte)
            {
                str = new TwoByteCharStream(file_name);
            }
            else if (old)
            {
                FileStream fs = new FileStream(file_name, FileMode.Open);
                str = new Antlr4.Runtime.AntlrInputStream(fs);
            }
            else if (encoding == null)
            {
                str = CharStreams.fromPath(file_name);
            }
            else
            {
                str = CharStreams.fromPath(file_name, encoding);
            }
        }
Exemple #35
0
    static Antlr4.Runtime.Tree.IParseTree DoParse(string input)
    {
        bool   show_tree   = false;
        bool   show_tokens = false;
        bool   old         = false;
        bool   two_byte    = false;
        string file_name   = null;

        System.Text.Encoding encoding = null;
        ICharStream          str      = null;

        if (input == null && file_name == null)
        {
            str = CharStreams.fromStream(System.Console.OpenStandardInput());
        }
        else if (input != null)
        {
            str = CharStreams.fromString(input);
        }
        else if (file_name != null)
        {
            if (two_byte)
            {
                str = new TwoByteCharStream(file_name);
            }
            else if (old)
            {
                FileStream fs = new FileStream(file_name, FileMode.Open);
                str = new Antlr4.Runtime.AntlrInputStream(fs);
            }
            else if (encoding == null)
            {
                str = CharStreams.fromPath(file_name);
            }
            else
            {
                str = CharStreams.fromPath(file_name, encoding);
            }
        }
        var lexer = new ArithmeticLexer(str);

        if (show_tokens)
        {
            StringBuilder new_s = new StringBuilder();
            for (int i = 0; ; ++i)
            {
                var ro_token = lexer.NextToken();
                var token    = (CommonToken)ro_token;
                token.TokenIndex = i;
                new_s.AppendLine(token.ToString());
                if (token.Type == Antlr4.Runtime.TokenConstants.EOF)
                {
                    break;
                }
            }
            System.Console.Error.WriteLine(new_s.ToString());
            lexer.Reset();
        }
        var tokens          = new CommonTokenStream(lexer);
        var parser          = new ArithmeticParser(tokens);
        var listener_lexer  = new ErrorListener <int>();
        var listener_parser = new ErrorListener <IToken>();

        lexer.AddErrorListener(listener_lexer);
        parser.AddErrorListener(listener_parser);
        DateTime before = DateTime.Now;

        ArithmeticParser.File_Context tree = parser.file_();
        DateTime after = DateTime.Now;

        System.Console.Error.WriteLine("Time: " + (after - before));
        if (listener_lexer.had_error || listener_parser.had_error)
        {
            System.Console.Error.WriteLine("Parse failed.");
        }
        else
        {
            System.Console.Error.WriteLine("Parse succeeded.");
        }
        if (show_tree)
        {
            System.Console.Error.WriteLine(tree.ToStringTree(parser));
        }
        System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0);
        return(tree);
    }
        private static Document Build(
            AntlrDocumentBuilder antlrDocumentBuilder,
            string data )
        {
            using( MemoryStream memoryStream = new MemoryStream( Encoding.UTF8.GetBytes( data ) ) )
            using( StreamReader streamReader = new StreamReader( memoryStream ) )
            {
                AntlrInputStream inputStream = new AntlrInputStream( streamReader );

                GraphQLLexer lexer = new GraphQLLexer( inputStream );
                if( antlrDocumentBuilder.LexerErrorListeners != null )
                {
                    lexer.RemoveErrorListeners();
                    antlrDocumentBuilder.LexerErrorListeners.Apply( x => lexer.AddErrorListener( x ) );
                }

                CommonTokenStream tokenStream = new CommonTokenStream( lexer );
                GraphQLParser parser = new GraphQLParser( tokenStream );
                if( antlrDocumentBuilder.ParserErrorListeners != null )
                {
                    parser.RemoveErrorListeners();
                    antlrDocumentBuilder.ParserErrorListeners.Apply( x => parser.AddErrorListener( x ) );
                }

                GraphQLVisitor vistor = new GraphQLVisitor();

                return vistor.Visit( parser.document() ) as Document;
            }
            
        }
Exemple #37
-1
        private void btnParse_Click(object sender, EventArgs e)
        {
            txtOutput.Clear();
            string inputString = txtInput.Text;
            MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? ""));

            AntlrInputStream input = new AntlrInputStream(inputStream);
            QLLexer lexer = new QLLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            QLParser parser = new QLParser(tokens);

            lexer.RemoveErrorListeners();
            parser.RemoveErrorListeners();

            lexer.AddErrorListener(new LexerErrorListener(){OnError = WriteError});
            parser.AddErrorListener(new ParserErrorListener(){OnError = WriteError});

            IParseTree tree = parser.questionnaire();

            //if (parser.NumberOfSyntaxErrors > 0)
            //    txtOutput.Text += string.Format("Parser errors found: {0}", parser.NumberOfSyntaxErrors);

            QLVisitor visitor = new QLVisitor();
            visitor.Visit(tree);

            txtOutput.Text += string.Format(@"{0}{0} Generated parse tree:
                                              {0} {1}
                                              {0} {2}"
                                                , Environment.NewLine
                                                , tree.ToStringTree(parser)
                                                , visitor.Visit(tree));
        }
Exemple #38
-1
        public static CsharpParseResults InvokeParse(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                return null;
            if (!System.IO.File.Exists(fileName))
                return null;

            var tr = System.IO.File.OpenRead(fileName);
            var input = new AntlrInputStream(tr);
            var lexer = new CSharp4Lexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new CSharp4Parser(tokens);

            var tree = parser.compilation_unit();

            var walker = new ParseTreeWalker();
            var loader = new CsharpParseTree();

            walker.Walk(loader, tree);

            var results = loader.Results;

            tr.Close();

            return results;
        }
Exemple #39
-1
        internal void LoadCommandStack(string Script)
        {

            // Clear the current stack //
            this.Commands.Clear();
            this._CompileErrorMessages.Clear();

            // Create a token stream and do lexal analysis //
            AntlrInputStream TextStream = new AntlrInputStream(Script);
            HScriptLexer HorseLexer = new HScriptLexer(TextStream);

            // Parse the script //
            CommonTokenStream HorseTokenStream = new CommonTokenStream(HorseLexer);
            HScriptParser HorseParser = new HScriptParser(HorseTokenStream);
            HorseParser.RemoveErrorListeners();
            HorseParser.AddErrorListener(new ParserErrorListener());
            
            // Create an executer object //
            CommandVisitor processor = new CommandVisitor(this.Home);

            // Load the call stack //
            try
            {
                foreach (HScriptParser.CommandContext context in HorseParser.compile_unit().command_set().command())
                {
                    this.Commands.Add(context);
                }
            }
            catch (Exception e)
            {
                this._CompileErrorMessages.Add(e.Message);
            }

        }