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); } ) { }
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); } ) { }
public Symbol(string name) : base( inp => (from sym in Tok.Lexeme <ImmutableList <ParserChar> >(Prim.String(name)) select new SymbolToken(sym)) .Parse(inp) ) { }
public Natural() : base( inp => (from lex in Tok.Lexeme(new Int()) select lex) .Fail("natural") .Parse(inp) ) { }
public Integer() : base( inp => (from lex in Tok.Lexeme(new Int()) select lex) .Fail("integer") .Parse(inp) ) { }
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); }
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) ) { }
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) ) { }
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) ) { }
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) ) { }
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) ) { }
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]; }