Example #1
0
        /// <summary>
        /// Adds a name to the item.  This defines the left-hand-side of the 
        /// statement.
        /// </summary>
        /// <param name="name">The parse item that defines the name.</param>
        /// <exception cref="System.ArgumentNullException">If name is null.</exception>
        public void AddName(IParseVariable name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            names.Add(name);
        }
Example #2
0
        /// <summary>
        /// Adds a name to the item.  This defines the left-hand-side of the
        /// statement.
        /// </summary>
        /// <param name="name">The parse item that defines the name.</param>
        /// <exception cref="System.ArgumentNullException">If name is null.</exception>
        public void AddName(IParseVariable name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            names.Add(name);
        }
Example #3
0
        /// <summary>
        /// Reads an assignment statement from the input.  The input is currently after the first name,
        /// on the comma or equal sign.  The debug token contains the name.
        /// </summary>
        /// <param name="input">Where to read input from.</param>
        /// <param name="debug">The first name.</param>
        /// <param name="local">True if this is a local definition, otherwise false.</param>
        /// <param name="variable">The first variable that was read.</param>
        /// <returns>The statement that was read.</returns>
        protected virtual AssignmentItem _readAssignment(Lexer input, Token debug, bool local,
                                                         IParseVariable variable)
        {
            var names = new List <IParseVariable>()
            {
                variable
            };

            while (input.ReadIfType(TokenType.Comma))
            {
                var curDebug = input.Peek();
                var exp      = _readExp(input, out _);
                if ((local && !(exp is NameItem)) || (!local && !(exp is IParseVariable)))
                {
                    throw new SyntaxException(Resources.NameOrExpForVar, input.Name, curDebug);
                }
                names.Add((IParseVariable)exp);
            }

            bool isParentheses = false;
            var  exps          = new List <IParseExp>();

            if (input.ReadIfType(TokenType.Assign))
            {
                exps.Add(_readExp(input, out isParentheses));

                while (input.ReadIfType(TokenType.Comma))
                {
                    exps.Add(_readExp(input, out isParentheses));
                }
            }
            else if (!local)
            {
                throw input.SyntaxError(string.Format(Resources.InvalidDefinition, "assignment"));
            }

            return(new AssignmentItem(names.ToArray(), exps.ToArray())
            {
                Debug = debug,
                Local = local,
                IsLastExpressionSingle = isParentheses,
            });
        }
Example #4
0
 /// <summary>
 /// Creates a new FuncDefItem with the given name.
 /// </summary>
 /// <param name="name">The name of the method, must be a NameItem
 /// or IndexerItem.</param>
 /// <param name="local">True if this is a local definition, otherwise false.</param>
 public FuncDefItem(IParseVariable name, bool local)
 {
     this.Prefix = name;
     this.args = new List<NameItem>();
     this.Local = local;
 }
Example #5
0
 /// <summary>
 /// Creates a new FuncDefItem with the given name.
 /// </summary>
 /// <param name="name">The name of the method, must be a NameItem or 
 /// IndexerItem.</param>
 public FuncDefItem(IParseVariable name)
     : this(name, false) { }
Example #6
0
        /// <summary>
        /// Reads a function from the input.  Input must be on the word 'function'.  If canName is true,
        /// it will give the function the read name; otherwise it will give it a null name.
        /// </summary>
        /// <param name="input">Where to read input from.</param>
        /// <param name="canName">True if the function can have a name, otherwise false.</param>
        /// <param name="local">True if this function is a local definition, otherwise false.</param>
        /// <returns>The function definition that was read.</returns>
        protected virtual FuncDefItem _readFunctionHelper(Lexer input, bool canName, bool local)
        {
            Token debug = input.Expect(TokenType.Function);

            IParseVariable name     = null;
            string         instName = null;

            if (input.PeekType(TokenType.Identifier))
            {
                Token temp = input.Expect(TokenType.Identifier);
                name = new NameItem(temp.Value)
                {
                    Debug = temp
                };
                while (input.ReadIfType(TokenType.Indexer))
                {
                    temp = input.Expect(TokenType.Identifier);
                    var literal = new LiteralItem(temp.Value)
                    {
                        Debug = temp
                    };
                    name = new IndexerItem(name, literal)
                    {
                        Debug = name.Debug
                    };
                }

                if (input.ReadIfType(TokenType.Colon))
                {
                    instName = input.Expect(TokenType.Identifier).Value;
                }
            }
            if (name != null && !canName)
            {
                throw new SyntaxException(Resources.FunctionCantHaveName, input.Name, debug);
            }
            if (name == null && canName)
            {
                throw new SyntaxException("Function statements must provide name", input.Name, debug);
            }

            var args = new List <NameItem>();

            input.Expect(TokenType.BeginParen);
            if (!input.PeekType(TokenType.EndParen))
            {
                do
                {
                    Token temp = input.PeekType(TokenType.Elipsis) ?
                                 input.Expect(TokenType.Elipsis) :
                                 input.Expect(TokenType.Identifier);
                    args.Add(new NameItem(temp.Value)
                    {
                        Debug = temp
                    });
                    if (temp.Value == "...")
                    {
                        break;
                    }
                } while (input.ReadIfType(TokenType.Comma));
            }
            input.Expect(TokenType.EndParen);
            BlockItem chunk = _readBlock(input);

            input.Expect(TokenType.End);
            chunk.Return ??= new ReturnItem();

            return(new FuncDefItem(args.ToArray(), chunk)
            {
                Debug = debug,
                InstanceName = instName,
                Prefix = name,
                Local = local,
            });
        }
Example #7
0
 /// <summary>
 /// Creates a new FuncDefItem with the given name.
 /// </summary>
 /// <param name="name">The name of the method, must be a NameItem
 /// or IndexerItem.</param>
 /// <param name="local">True if this is a local definition, otherwise false.</param>
 public FuncDefItem(IParseVariable name, bool local)
 {
     this.Prefix = name;
     this.args   = new List <NameItem>();
     this.Local  = local;
 }
Example #8
0
 /// <summary>
 /// Creates a new FuncDefItem with the given name.
 /// </summary>
 /// <param name="name">The name of the method, must be a NameItem or
 /// IndexerItem.</param>
 public FuncDefItem(IParseVariable name)
     : this(name, false)
 {
 }
Example #9
0
        /// <summary>
        /// Reads an assignment statement from the input.  The input is currently
        /// after the first name, on the comma or equal sign.  The debug token
        /// contains the name and should contain the entire statement.
        /// </summary>
        /// <param name="input">Where to read input from.</param>
        /// <param name="debug">Currently contains the first name, and after
        /// should contain the entire statement.</param>
        /// <param name="local">True if this is a local definition, otherwise false.</param>
        /// <param name="variable">The first variable that was read.</param>
        /// <returns>The statement that was read.</returns>
        protected virtual AssignmentItem ReadAssignment(ITokenizer input, ref Token debug, bool local, IParseVariable variable)
        {
            // read each of the variable names
            AssignmentItem assign = new AssignmentItem(local);
            assign.AddName(variable);
            while (input.Peek().Value == ",")
            {
                Read(input, ref debug); // read ','

                // read the left-hand-expression
                var exp = ReadExp(input, ref debug);
                if ((local && !(exp is NameItem)) || (!local && !(exp is IParseVariable)))
                    throw new SyntaxException(Resources.NameOrExpForVar, input.Name, debug);
                assign.AddName((IParseVariable)exp);
            }

            // read the initial values
            if (input.Peek().Value == "=")
            {
                Read(input, ref debug); // read '='
                assign.AddItem(ReadExp(input, ref debug));

                while (input.Peek().Value == ",")
                {
                    Read(input, ref debug); // read ','
                    assign.AddItem(ReadExp(input, ref debug));
                }
            }
            else if (!local)
                throw new SyntaxException(
                    string.Format(Resources.InvalidDefinition, "assignment"),
                    input.Name, debug);

            assign.Debug = debug;
            return assign;
        }