Ejemplo n.º 1
0
        public Identifier(GeneralLanguageDef languageDef)
            :
            base(
                inp =>
        {
            var res = Tok.Lexeme(
                from name in new Ident(languageDef)
                where !IdentHelper.IsReservedName(name, languageDef)
                select new IdentifierToken(name, inp.First().Location)
                )
                      .Parse(inp);

            if (res.IsFaulted)
            {
                return(res);
            }

            if (res.Value.IsEmpty)
            {
                return(ParserResult.Fail <IdentifierToken>("unexpected: reserved word", inp));
            }

            return(res);
        }
                )
        { }
Ejemplo n.º 2
0
        public Operator(GeneralLanguageDef languageDef)
            :
            base(
                inp =>
        {
            var res = Tok.Lexeme(
                from name in new Oper(languageDef)
                where !OpsHelper.IsReservedOp(name, languageDef)
                select new OperatorToken(name, inp.First().Location))
                      .Parse(inp);

            if (res.IsFaulted)
            {
                return(res);
            }

            if (res.Value.IsEmpty)
            {
                return(ParserResult.Fail <OperatorToken>("unexpected: reserved operator", inp));
            }

            return(res);
        }
                )
        { }
Ejemplo n.º 3
0
 public Symbol(string name)
     :
     base(
         inp => (from sym in Tok.Lexeme <ImmutableList <ParserChar> >(Prim.String(name))
                 select new SymbolToken(sym))
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 4
0
 public Natural()
     :
     base(
         inp => (from lex in Tok.Lexeme(new Int())
                 select lex)
         .Fail("natural")
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 5
0
 public Integer()
     :
     base(
         inp => (from lex in Tok.Lexeme(new Int())
                 select lex)
         .Fail("integer")
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 6
0
        public void LexemeTest()
        {
            var lex = from l in Tok.Lexeme <ParserChar>(Prim.Character('A'))
                      select l;

            var res = lex.Parse("A");

            Assert.True(!res.IsFaulted);

            res = lex.Parse("A   ");
            Assert.True(!res.IsFaulted);
        }
Ejemplo n.º 7
0
 public Reserved(string name, GeneralLanguageDef languageDef)
     :
     base(
         inp => Tok.Lexeme(
             from cs in IdentHelper.CaseString(name, languageDef)
             from nf in Prim.NotFollowedBy(languageDef.IdentLetter)
             .Fail("end of " + cs.AsString())
             select new ReservedToken(cs, inp.Head().Location)
             )
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 8
0
 public Int()
     :
     base(
         inp => (from f in Tok.Lexeme <ParserChar>(new Sign())
                 from n in new Nat()
                 select new IntegerToken(
                     f.Value == '-' ? -n.Value : n.Value,
                     f.Location
                     ))
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 9
0
 public ReservedOp(string name, GeneralLanguageDef languageDef)
     :
     base(
         inp => Tok.Lexeme(
             from op in Prim.String(name)
             from nf in Prim.NotFollowedBy(languageDef.OpLetter)
             .Fail("end of " + op.AsString())
             select new ReservedOpToken(op, inp.First().Location)
             )
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 10
0
 public CharLiteral()
     :
     base(
         inp => Tok.Lexeme(
             Prim.Between(
                 Prim.Character('\''),
                 Prim.Character('\'').Fail("end of character"),
                 new CharacterChar()
                 ))
         .Select(ch => new CharLiteralToken(ch, inp.Head().Location))
         .Fail("character")
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 11
0
 public StringLiteral()
     :
     base(
         inp => (from l in Tok.Lexeme(
                     from str in Prim.Between(
                         Prim.Character('"'),
                         Prim.Character('"').Fail("end of string"),
                         Prim.Many(new StringChar())
                         )
                     select str
                     )
                 select new StringLiteralToken(l, inp.Head().Location)
                 )
         .Fail("literal string")
         .Parse(inp)
         )
 {
 }
Ejemplo n.º 12
0
        public GenTokenParser(LanguageDef def)
        {
            Identifier    = Tok.Id.Identifier(def);
            reserved      = def.ReservedNames.ToDictionary(name => name, name => Tok.Id.Reserved(name, def) as Parser <ReservedToken>);
            Operator      = Tok.Ops.Operator(def);
            reservedOp    = def.ReservedOpNames.ToDictionary(name => name, name => Tok.Ops.ReservedOp(name, def) as Parser <ReservedOpToken>);
            CharLiteral   = Tok.Chars.CharLiteral();
            StringLiteral = Tok.Strings.StringLiteral();
            Natural       = Tok.Numbers.Natural();
            Integer       = Tok.Numbers.Integer();

            // floating = Tok.Numbers.Floating(); TODO
            // naturalOrFloat = Tok.Numbers.NaturalOrFloating(); TODO

            WhiteSpace  = Tok.WhiteSpace(def);
            Decimal     = Tok.Numbers.Decimal();
            Hexadecimal = Tok.Numbers.Hexadecimal();
            Octal       = Tok.Numbers.Octal();
            Symbol      = (string name) => Tok.Symbol(name);
            Lexeme      = (Parser <A> p) => Tok.Lexeme(p);

            Parens   = (Parser <A> p) => Tok.Bracketing.Parens(p);
            Braces   = (Parser <A> p) => Tok.Bracketing.Braces(p);
            Angles   = (Parser <A> p) => Tok.Bracketing.Angles(p);
            Brackets = (Parser <A> p) => Tok.Bracketing.Brackets(p);

            Semi      = Tok.Symbol(";");
            Comma     = Tok.Symbol(",");
            Colon     = Tok.Symbol(":");
            Dot       = Tok.Symbol(".");
            CommaSep  = (Parser <A> p) => Prim.SepBy(p, Comma);
            SemiSep   = (Parser <A> p) => Prim.SepBy(p, Semi);
            CommaSep1 = (Parser <A> p) => Prim.SepBy1(p, Comma);
            SemiSep1  = (Parser <A> p) => Prim.SepBy1(p, Semi);

            Reserved   = name => reserved[name];
            ReservedOp = name => reservedOp[name];
        }