Example #1
0
    protected List <object> performSyntaxAnalysis(
        TextReader inR,
        string sourceName,
        StreamWriter err,
        Type nodeClass,
        HashSet <Exception> pErrors)
    {
        OCLWorkbenchLexer  lexer  = new OCLWorkbenchLexer(inR, sourceName, err, pErrors);
        OCLWorkbenchParser parser = new OCLWorkbenchParser(sourceName, lexer, err, pErrors);

        List <object>        result            = null;
        OCLCompilerException compilerException = null;

        try {
            result = parseNode(parser, nodeClass);
        } catch (antlr.RecognitionException e) {
            compilerException = new OCLSyntaticException(e.Message,
                                                         new SourceLocation(parser.getFilename(), e.getLine(),
                                                                            e.getColumn()));
        } catch (antlr.TokenStreamRecognitionException e) {
            compilerException = new OCLSyntaticException(e.Message,
                                                         new SourceLocation(parser.getFilename(), e.recog.getLine(),
                                                                            e.recog.getColumn()));
        } catch (antlr.TokenStreamException e) {
            compilerException = new OCLSyntaticException(e.Message,
                                                         new SourceLocation(parser.getFilename(), -1, -1));
        } catch (Exception e) {
            if (e.Message != null)
            {
                compilerException = new OCLCompilerException(e.Message, new SourceLocation(parser.getFilename(), -1, -1));
            }
        } finally {
            if ((err != null) && (compilerException != null))
            {
                errors.Add(compilerException);
                syntaticErrorsCount++;

                err.WriteLine(compilerException);
                err.Flush();
            }
        }

        syntaticErrorsCount += parser.getErrorCount();

        if ((err != null) &&
            ((syntaticErrorsCount != 0) || (compilerException != null)))
        {
            err.Flush();
        }

        return(result);
    }
Example #2
0
    protected List <object> parseNode(OCLWorkbenchParser parser, Type nodeClass)
    {
        List <object> result = null;
        CSTNode       node   = null;

        if (nodeClass == null)
        {
            result = parser.expressionStream();
        }
        else if (nodeClass == typeof(CSTExpressionInOclCS))
        {
            node = parser.expressionInOCLCS();
        }
        else if (nodeClass == typeof(CSTTypeCS))
        {
            node = parser.typeCS();
        }
        else if (nodeClass == typeof(CSTContextDeclarationCS))
        {
            node = parser.contextDeclarationCS();
        }
        else if (nodeClass == typeof(CSTArgumentCS))
        {
            List <object> arguments = parser.argumentsCS();
            node = (CSTNode)arguments[0];
        }
        else if (nodeClass == typeof(CSTLiteralExpCS))
        {
            node = parser.literalExpCS();
        }
        else if (nodeClass == typeof(CSTVariableDeclarationCS))
        {
            node = parser.variableDeclarationCS();
        }
        else if (nodeClass == typeof(CSTVariableDeclarationCS))
        {
            node = parser.variableDeclarationCS();
        }
        else if (nodeClass == typeof(CSTClassifierAttributeCallExpC))
        {
            node = parser.classifierAttributeCallExpCS();
        }

        if ((result == null) && (node != null))
        {
            result = new List <object>();
            result.Add(node);
        }

        return(result);
    }
        private CSTNode getClassifierRootNode(String source, String inputName)
        {
            OCLWorkbenchParser parser;
            StreamWriter       err;

            StringReader inR    = new StringReader(source);
            String       inName = inputName;

            err = new StreamWriter(Console.OpenStandardOutput());
            err.Flush();

            HashSet <Exception> errorsList = new HashSet <Exception>();
            OCLWorkbenchLexer   lexer      = new OCLWorkbenchLexer(inR, inName, err, errorsList);

            parser = new OCLWorkbenchParser(inName, lexer, err, errorsList);

            CSTNode result = null;

            try {
                result = parser.contextDeclarationCS();
            } catch (antlr.RecognitionException e) {
                err.WriteLine(parser.getFilename() + ":" +
                              "[" + e.getLine() + ", " + e.getColumn() + "]: " +
                              e.Message);
                throw e;
            } catch (antlr.TokenStreamRecognitionException e) {
                err.WriteLine(parser.getFilename() + ":" +
                              "[" + e.recog.getLine() + ", " + e.recog.getColumn() + "]: " +
                              e.recog.Message);
                throw e;
            } catch (antlr.TokenStreamException ex) {
                err.WriteLine(parser.getFilename() + ":" + ex.Message);
                throw ex;
            }   finally {
                err.Flush();
            }

            if (parser.getErrorCount() != 0)
            {
                throw new Exception("syntatic errors in compilation");
            }

            return(result);
        }