Exemplo n.º 1
0
        private static Atom ParseString(StringIterator Iterator)
        {
            var stringValue = "";

            Iterator.Advance();
            while (!Iterator.AtEnd && Iterator.Next != '\"')
            {
                stringValue += Iterator.Next;
                Iterator.Advance();

                //Todo: Escape chars.
            }

            if (Iterator.AtEnd)
            {
                throw new ParseError("Unexpected end of input in string literal.", Iterator.place);
            }

            Iterator.Advance();

            if (!Iterator.AtEnd && !" \t\n\r)".Contains(Iterator.Next))
            {
                throw new ParseError("Expected end of token", Iterator.place);
            }

            return(new StringAtom {
                Value = stringValue
            });
        }
Exemplo n.º 2
0
 private static void SkipWhitespace(this StringIterator Iterator)
 {
     while (!Iterator.AtEnd && " \t\r\n".Contains(Iterator.Next))
     {
         Iterator.Advance();
     }
     //if (Iterator.AtEnd)
     //    throw new ParseError("Unexpected end of input", Iterator.place);
 }
Exemplo n.º 3
0
        public static Atom Parse(StringIterator Iterator)
        {
            var result = ParseAtom(Iterator);

            if (!Iterator.AtEnd)
            {
                throw new ParseError("Did not consume all input", Iterator.place);
            }
            return(result);
        }
Exemplo n.º 4
0
        private static Atom ParseString(StringIterator Iterator)
        {
            var stringValue = "";

            Iterator.Advance();
            while (!Iterator.AtEnd && Iterator.Next != '\"')
            {
                if (Iterator.Next == '\\') //escape sequence
                {
                    Iterator.Advance();
                    if (Iterator.AtEnd)
                    {
                        throw new ParseError("Unexpected end of input in escape sequence.", Iterator.place);
                    }

                    if (Iterator.Next == 'n')
                    {
                        stringValue += '\n';
                    }
                    else if (Iterator.Next == '\\')
                    {
                        stringValue += '\\';
                    }
                    else
                    {
                        throw new ParseError("Unknown escape sequence.", Iterator.place);
                    }
                }
                else
                {
                    stringValue += Iterator.Next;
                }

                Iterator.Advance();
            }

            if (Iterator.AtEnd)
            {
                throw new ParseError("Unexpected end of input in string literal.", Iterator.place);
            }

            Iterator.Advance();

            if (!Iterator.AtEnd && !" \t\n\r.)".Contains(Iterator.Next))
            {
                throw new ParseError("Expected end of token", Iterator.place);
            }

            return(new StringAtom {
                Value = stringValue
            });
        }
Exemplo n.º 5
0
        private static Atom ParseToken(StringIterator Iterator)
        {
            var tokenValue = "";

            while (!Iterator.AtEnd && !" \t\r\n.()".Contains(Iterator.Next))
            {
                tokenValue += Iterator.Next;
                Iterator.Advance();
            }
            return(new TokenAtom {
                Value = tokenValue
            });
        }
Exemplo n.º 6
0
        private static Atom ParseAtom(StringIterator Iterator)
        {
            if (Iterator.AtEnd)
            {
                return(null);
            }

            while (Iterator.Next == ';')
            {
                while (!Iterator.AtEnd && Iterator.Next != '\n')
                {
                    Iterator.Advance();
                }
                Iterator.SkipWhitespace();
                if (Iterator.AtEnd)
                {
                    return(null);
                }
            }

            var  modifier = Modifier.None;
            Atom result   = null;

            if (Iterator.Next == '\'')
            {
                Iterator.Advance();
                modifier = Modifier.Quote;
            }
            else if (Iterator.Next == '$')
            {
                Iterator.Advance();
                modifier = Modifier.Expand;
            }
            else if (Iterator.Next == ':')
            {
                Iterator.Advance();
                modifier = Modifier.Evaluate;
            }

            if (Iterator.AtEnd)
            {
                throw new ParseError("Unexpected end of input", Iterator.place);
            }

            if (Iterator.Next == '(')
            {
                Iterator.Advance();
                Iterator.SkipWhitespace();

                var list = new ListAtom {
                    Value = new List <Atom>()
                };

                while (!Iterator.AtEnd && Iterator.Next != ')')
                {
                    list.Value.Add(ParseAtom(Iterator));
                    Iterator.SkipWhitespace();
                }

                Iterator.Advance();
                result = list;
            }
            else if (Iterator.Next == '[')
            {
                if (modifier != Modifier.None)
                {
                    throw new ParseError("Modifiers not allowed on record.", Iterator.place);
                }

                Iterator.Advance();
                Iterator.SkipWhitespace();

                var record = new RecordAtom();
                while (!Iterator.AtEnd && Iterator.Next != ']')
                {
                    var listItem = ParseAtom(Iterator) as ListAtom;
                    if (listItem == null)
                    {
                        throw new ParseError("Expected list inside record", Iterator.place);
                    }
                    if (listItem.Value.Count != 2)
                    {
                        throw new ParseError("Malformed record", Iterator.place);
                    }
                    var entryName = listItem.Value[0] as TokenAtom;
                    if (entryName == null)
                    {
                        throw new ParseError("Malformed record", Iterator.place);
                    }
                    record.Variables.Upsert(entryName.Value, listItem.Value[1]);
                }

                Iterator.Advance();
                record.Literal = true;
                result         = record;
            }
            else if ("0123456789-".Contains(Iterator.Next))
            {
                if (modifier != Modifier.None)
                {
                    throw new ParseError("Modifiers not allowed on literals.", Iterator.place);
                }
                result = ParseNumber(Iterator);
            }
            else if (Iterator.Next == '\"')
            {
                if (modifier != Modifier.None)
                {
                    throw new ParseError("Modifiers not allowed on literals.", Iterator.place);
                }
                result = ParseString(Iterator);
            }
            else
            {
                result = ParseToken(Iterator);
                if (result.Type == AtomType.Token && (result as TokenAtom).Value == "nil")
                {
                    result = new NilAtom();
                }
            }


            Iterator.SkipWhitespace();

            if (!Iterator.AtEnd && Iterator.Next == '.')
            {
                Iterator.Advance();
                Iterator.SkipWhitespace();
                var member = ParseAtom(Iterator);

                if (member.Modifier == Modifier.Expand)
                {
                    throw new ParseError("Modifier illegal in this context", Iterator.place);
                }

                result = TransformMemberAccess(new MemberAtom
                {
                    Lhs = result,
                    Rhs = member
                });
            }

            result.Modifier = modifier;

            return(result);
        }
Exemplo n.º 7
0
        private static Atom ParseNumber(StringIterator Iterator)
        {
            var  numberString = "";
            bool decimalFound = false;
            bool negate       = false;

            if (Iterator.Next == '-')
            {
                negate = true;
                Iterator.Advance();
            }

            if (Iterator.AtEnd || !"0123456789".Contains(Iterator.Next))
            {
                Iterator.Rewind();
                return(ParseToken(Iterator));
            }

            while (!Iterator.AtEnd && "0123456789".Contains(Iterator.Next))
            {
                numberString += Iterator.Next;
                Iterator.Advance();
            }

            if (!Iterator.AtEnd && Iterator.Next == '.')
            {
                decimalFound  = true;
                numberString += '.';
                Iterator.Advance();

                while (!Iterator.AtEnd && "0123456789".Contains(Iterator.Next))
                {
                    numberString += Iterator.Next;
                    Iterator.Advance();
                }
            }

            if (!Iterator.AtEnd && !" \t\n\r.)".Contains(Iterator.Next))
            {
                throw new ParseError("Expected end of token", Iterator.place);
            }

            if (decimalFound)
            {
                var number = Convert.ToSingle(numberString);
                if (negate)
                {
                    number = -number;
                }
                return(new DecimalAtom {
                    Value = number
                });
            }
            else
            {
                var number = Convert.ToInt32(numberString);
                if (negate)
                {
                    number = -number;
                }
                return(new IntegerAtom {
                    Value = number
                });
            }
        }