Exemplo n.º 1
0
 private void initialise(GingerToken type, Variable name, ImportList il, FunctionList fl)
 {
     _type = type;
     add(name);
     add(il);
     add(fl);
 }
Exemplo n.º 2
0
        //private Type parseType()
        //{
        //    Type type;
        //    if (currentScannerToken == GingerToken.Int)
        //    {
        //        type = new Integer(scanner.row, scanner.col);
        //    }
        //    else if (currentScannerToken == GingerToken.Bool)
        //    {
        //        type = new Boolean(scanner.row, scanner.col);
        //    }
        //    else if (currentScannerToken == GingerToken.Void)
        //    {
        //        type = new Void(scanner.row, scanner.col);
        //    }
        //    else if (currentScannerToken == GingerToken.Contract)
        //    {
        //        type = new Contract(scanner.row, scanner.col);
        //    }
        //    else if (currentScannerToken == GingerToken.Implementation)
        //    {
        //        type = new Implementation(scanner.row, scanner.col);
        //    }
        //    else
        //    {
        //        throw new NotImplementedException();
        //    }

        //    return type;
        //}

        private GingerToken parseAnnotation(GingerToken defaultAnnotation)
        {
            spyScannerToken();
            if (spiedScannerToken == GingerToken.OpenAnnotation)
            {
                nextScannerToken();
                nextScannerToken();
                if (currentScannerToken == GingerToken.Annotation)
                {
                    nextScannerToken();
                    return(currentScannerToken);
                }
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Annotation.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
            }
            else
            {
                return(defaultAnnotation);
            }
        }
Exemplo n.º 3
0
        // precedence-climbing method
        // https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method
        private Node parseExpression(Node lhs, int minPrecedence)
        {
            spyScannerToken();
            // the spied token is an operator and has precedence >= the minimumum precedence
            while (isOperator(spiedScannerToken.GetValueOrDefault()) && OPERATOR_PRECEDENCE[spiedScannerToken.GetValueOrDefault()].precedence >= minPrecedence)
            {
                GingerToken op = spiedScannerToken.GetValueOrDefault();
                // move to spied position
                nextScannerToken();

                nextScannerToken();
                Node rhs = parseTerminalExpression();

                spyScannerToken();
                // the spied token is an operator, and its precedence is greater than the previous op's precedence OR it is a right associated operator with precedence equal to op's precedence
                while (isOperator(spiedScannerToken.GetValueOrDefault()) && (OPERATOR_PRECEDENCE[spiedScannerToken.GetValueOrDefault()].precedence > OPERATOR_PRECEDENCE[op].precedence || (OPERATOR_PRECEDENCE[spiedScannerToken.GetValueOrDefault()].rightAssociated && OPERATOR_PRECEDENCE[spiedScannerToken.GetValueOrDefault()].precedence == OPERATOR_PRECEDENCE[op].precedence)))
                {
                    rhs = parseExpression(rhs, OPERATOR_PRECEDENCE[spiedScannerToken.GetValueOrDefault()].precedence);
                    spyScannerToken();
                }

                if (Grammar.isBinaryOperator(op))
                {
                    lhs = new BinaryOperation(op, lhs, rhs);
                }
                //else if (Grammar.isConditionOperator(op))
                //{
                //    lhs = new InequalityOperation(op, lhs, rhs);
                //}
                else
                {
                    throw new ParseException(scanner.row, scanner.col, $"Expected '{GingerToken.Addition.ToString()}',  found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR);
                }
            }

            return(lhs);
        }
Exemplo n.º 4
0
 public Operation(GingerToken op, Node lhs, Node rhs) : base()
 {
     this._op = op;
     this.add(lhs);
     this.add(rhs);
 }
Exemplo n.º 5
0
 public Component(GingerToken type, Variable name, ImportList il, FunctionList fl, Identifier extends) : base()
 {
     initialise(type, name, il, fl);
     add(extends);
 }
Exemplo n.º 6
0
        //public static bool isControl(GingerToken token)
        //{
        //    return token == GingerToken.If;
        //}

        public static bool isType(GingerToken token)
        {
            //return token == GingerToken.Int || token == GingerToken.Bool;
            return(token == GingerToken.Var);
        }
Exemplo n.º 7
0
 private bool isOperator(GingerToken token)
 {
     return(Grammar.isBinaryOperator(token));
 }
Exemplo n.º 8
0
 public Sink(GingerToken securityLevel, Node expr) : base(securityLevel)
 {
     this.add(expr);
 }
Exemplo n.º 9
0
 private bool isOperator(GingerToken token)
 {
     return Grammar.isBinaryOperator(token) || Grammar.isConditionOperator(token);
 }
Exemplo n.º 10
0
 public static bool isType(GingerToken token)
 {
     return token == GingerToken.Int || token == GingerToken.Bool;
 }
Exemplo n.º 11
0
 public static bool isControl(GingerToken token)
 {
     return token == GingerToken.If || token == GingerToken.While;
 }
Exemplo n.º 12
0
 public static bool isConditionOperator(GingerToken token)
 {
     return token == GingerToken.LessThan || token == GingerToken.GreaterThan;
 }
Exemplo n.º 13
0
 public static bool isBinaryOperator(GingerToken token)
 {
     return token == GingerToken.Addition || token == GingerToken.Subtraction;
 }
Exemplo n.º 14
0
 public static bool isComponent(GingerToken token)
 {
     return(token == GingerToken.Contract || token == GingerToken.Implementation);
 }
Exemplo n.º 15
0
 public static bool isInformationEndpoint(GingerToken token)
 {
     return(token == GingerToken.Source || token == GingerToken.Sink);
 }
Exemplo n.º 16
0
 public InformationEndpoint(GingerToken securityLevel)
 {
     this._securityLevel = securityLevel;
 }
Exemplo n.º 17
0
 public Source(GingerToken securityLevel) : base(securityLevel)
 {
 }
Exemplo n.º 18
0
 private void nextScannerToken()
 {
     currentScannerToken = scanner.next();
 }
Exemplo n.º 19
0
 public BinaryOperation(GingerToken binaryOp, Node lhs, Node rhs) : base(binaryOp, lhs, rhs)
 {
     return;
 }
Exemplo n.º 20
0
        private StatementList parseStatementList(GingerToken endToken)
        {
            StatementList sl = new StatementList();
            do
            {
                try
                {
                    sl.add(parseStatement());
                }
                catch (ParseException pe)
                {
                    _errors.Add(pe);
                }

                nextScannerToken();

                if (currentScannerToken != endToken && currentScannerToken == GingerToken.EndOfFile)
                {
                    _errors.Add(new ParseException(scanner.row, scanner.col, $"Expected '{endToken.ToString()}', found '{currentScannerToken.ToString()}'", ExceptionLevel.ERROR));
                }
            } while (currentScannerToken != endToken && currentScannerToken != GingerToken.EndOfFile);

            return sl;
        }
Exemplo n.º 21
0
        public GingerToken next()
        {
            clearSemanticValue();

            // clear out any white space, we don't care about it
            while (Lexicon.isWhiteSpace(currentChar))
            {
                nextChar();
            }

            if (Lexicon.isStartNumberChar(currentChar))
            {
                do
                {
                    semanticValue.Add(currentChar);
                    nextChar();
                } while (Lexicon.isDigit(currentChar));

                return(GingerToken.Number);
            }
            else if (Lexicon.isStartAssignmentChar(currentChar))
            {
                nextChar();
                if (currentChar == Lexicon.ASSIGNMENT[1])
                {
                    nextChar();
                    return(GingerToken.Assignment);
                }
                else
                {
                    GingerToken spied = spy();
                    if (spied == GingerToken.Annotation)
                    {
                        return(GingerToken.OpenAnnotation);
                    }
                    else if (spied == GingerToken.Identifier)
                    {
                        return(GingerToken.Extends);
                    }
                }
            }
            else if (Lexicon.isStartKeywordOrIdentifierChar(currentChar))
            {
                do
                {
                    semanticValue.Add(currentChar);
                    nextChar();
                } while (Lexicon.isKeywordOrIdentifierChar(currentChar));

                //if (tokenValue.SequenceEqual(Lexicon.IF))
                //{
                //    return GingerToken.If;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.BOOLEAN_TRUE) || tokenValue.SequenceEqual(Lexicon.BOOLEAN_FALSE))
                //{
                //    return GingerToken.BooleanLiteral;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.INT))
                //{
                //    return GingerToken.Int;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.BOOL))
                //{
                //    return GingerToken.Bool;
                //}
                if (tokenValue.SequenceEqual(Lexicon.FUNCTION))
                {
                    return(GingerToken.Function);
                }
                //else if (tokenValue.SequenceEqual(Lexicon.COMPONENT))
                //{
                //    return GingerToken.Component;
                //}
                else if (tokenValue.SequenceEqual(Lexicon.RETURN))
                {
                    return(GingerToken.Return);
                }
                //else if (tokenValue.SequenceEqual(Lexicon.VOID))
                //{
                //    return GingerToken.Void;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.AS))
                //{
                //    return GingerToken.As;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.CONTRACT))
                //{
                //    return GingerToken.Contract;
                //}
                //else if (tokenValue.SequenceEqual(Lexicon.IMPLEMENTATION))
                //{
                //    return GingerToken.Implementation;
                //}
                else if (tokenValue.SequenceEqual(Lexicon.VAR_DECLARATION))
                {
                    return(GingerToken.Var);
                }
                //else if(tokenValue.SequenceEqual(Lexicon.REF))
                //{
                //    return GingerToken.Ref;
                //}
                else if (tokenValue.SequenceEqual(Lexicon.SEC_LVL_HIGH))
                {
                    return(GingerToken.High);
                }
                else if (tokenValue.SequenceEqual(Lexicon.SEC_LVL_LOW))
                {
                    return(GingerToken.Low);
                }
                else if (tokenValue.SequenceEqual(Lexicon.SYS_READ))
                {
                    return(GingerToken.Source);
                }
                else if (tokenValue.SequenceEqual(Lexicon.SYS_WRITE))
                {
                    return(GingerToken.Sink);
                }
                else if (tokenValue.SequenceEqual(Lexicon.CONTRACT))
                {
                    return(GingerToken.Contract);
                }
                else if (tokenValue.SequenceEqual(Lexicon.IMPLEMENTATION))
                {
                    return(GingerToken.Implementation);
                }
                else if (tokenValue.SequenceEqual(Lexicon.IMPORT))
                {
                    return(GingerToken.Import);
                }
                else
                {
                    return(GingerToken.Identifier);
                }
            }
            else if (currentChar == Lexicon.OPEN_PRECEDENT)
            {
                nextChar();
                return(GingerToken.OpenPrecedent);
            }
            else if (currentChar == Lexicon.CLOSE_PRECEDENT)
            {
                nextChar();
                return(GingerToken.ClosePrecedent);
            }
            else if (currentChar == Lexicon.OPEN_STATEMENT_LIST)
            {
                nextChar();
                return(GingerToken.OpenList);
            }
            else if (currentChar == Lexicon.CLOSE_STATEMENT_LIST)
            {
                nextChar();
                return(GingerToken.CloseList);
            }
            else if (currentChar == Lexicon.ANNOTATION)
            {
                nextChar();
                return(GingerToken.Annotation);
            }
            //else if (currentChar == Lexicon.END_OF_LINE)
            //{
            //    nextChar();
            //    return GingerToken.EndOfLine;
            //}
            else if (currentChar == Lexicon.ADDITION)
            {
                nextChar();
                return(GingerToken.Addition);
            }
            //else if (currentChar == Lexicon.SUBTRACTION)
            //{
            //    nextChar();
            //    return GingerToken.Subtraction;
            //}
            //else if (currentChar == Lexicon.LESS_THAN)
            //{
            //    nextChar();
            //    return GingerToken.LessThan;
            //}
            //else if (currentChar == Lexicon.GREATER_THAN)
            //{
            //    nextChar();
            //    return GingerToken.GreaterThan;
            //}
            //else if (currentChar == Lexicon.SECURITY_ASSIGNMENT)
            //{
            //    nextChar();
            //    return GingerToken.SecurityAssignment;
            //}
            else if (currentChar == EOF)
            {
                nextChar();
                return(GingerToken.EndOfFile);
            }
            else if (currentChar == Lexicon.LIST_SEPARATOR)
            {
                nextChar();
                return(GingerToken.ListSeparator);
            }
            else if (currentChar == Lexicon.IDENT_SEPARATOR)
            {
                nextChar();
                return(GingerToken.IdentifierSeparator);
            }

            nextChar();
            return(GingerToken.Unknown);
        }
Exemplo n.º 22
0
 public Component(GingerToken type, Variable name, ImportList il, FunctionList fl) : base()
 {
     initialise(type, name, il, fl);
 }
Exemplo n.º 23
0
 private void nextScannerToken()
 {
     currentScannerToken = scanner.next();
 }
Exemplo n.º 24
0
 public static bool isBinaryOperator(GingerToken token)
 {
     return(token == GingerToken.Addition);
 }