protected override void VisitClassExpression(ClassExpression expression) { CS.IndentInOut(false, "ClassExpression", () => { var characters = new SortedSet <int>(); foreach (var part in expression.Choices) { characters.UnionWith(part.Values); } var array = string.Join(",", characters.Select(c => CharRep.InCSharp(c))); var staticValue = GL.Local($"_{Cfg.CL("character-class")}"); GL.Ln($"private static int[] {staticValue} = new int[] {{{array}}};"); CS.Ln($"// {staticValue} = {{{array}}}"); var cond = expression.Negated ? "< 0" : ">= 0"; var check = characters.Count > 8 ? $"_current => Array.BinarySearch({staticValue}, _current) {cond}" : $"_current => Array.IndexOf({staticValue}, _current) {cond}"; CS.Ln($"{Locals.Result} = {Cfg.MatchPredicate}({Cfg.CurName}, {check});"); }); }
private static string Head(INode node) { if (node is ILeaf leaf) { return($"{leaf.Name}='{CharRep.InText(leaf.Value)}'"); } return($"{node.Name} [{string.Join(", ", node.Children.Select(c => c.Name))}]"); }
protected override void VisitStringLiteralExpression(StringLiteralExpression expression) { void Write() { Writer.Write($"'{CharRep.InText(expression.Value)}'"); } LexSpaced(expression, Write); }
public override string ToString() { if (Min == Max) { return(CharRep.InRange(Min)); } return($"{CharRep.InRange(Min)}-{CharRep.InRange(Max)}"); }
protected override void VisitStringLiteralExpression(StringLiteralExpression expression) { CS.IndentInOut( "StringLiteralExpression", () => { CS.Ln($"{Locals.Result} = {Cfg.MatchString}({Cfg.CurName}, \"{CharRep.InCSharp(expression.Value)}\");"); }); }
public override string ToString() { var text = CharRep.InText(Span.ToString()); if (text.Length > 80) { return(text.Substring(0, 80) + "..."); } return(text); }
public override string ToString() { if (Max == 0x10FFFF) { return($"{CharRep.InRange(Min)}-{CharRep.InRange(Max)}"); } var chars = Enumerable.Range(Min, Max - Min + 1).Select(n => CharRep.InRange(n)); return(string.Join(", ", chars)); }
protected override void VisitRule(IRule rule) { CS.Ln($"// {rule.Identifier} -> {Cfg.CU(rule.Identifier)}"); CS.Block($"protected virtual {Cfg.IResult} {Cfg.CU(rule.Identifier)}({Cfg.IContext} {Cfg.CtxName})", () => { Locals.Reset(); using (Locals.PrepareResult()) { CS.Ln($"var {Cfg.CurName} = {Cfg.CtxName};"); VisitExpression(rule.Expression); CS.Ln($"return {Cfg.FinishRule}({Locals.Result}, \"{CharRep.InCSharp(rule.Identifier.Name)}\");"); } }); }
public override string ToString() { return($"'{CharRep.Convert(Character)}'"); }
public override string ToString() { return($"'{CharRep.InText(Value)}'"); }
public string DD() { return($"[{CharRep.InRange(Min)}-{CharRep.InRange(Max)}]"); }
private Token NextToken() { clutter = current; nl = Skip(); while (true) { payload = current; Debug.Assert(current <= limit); if (current == limit) { return(Token(TK.Eof)); } switch (Current) { case '"': return(String()); case '\'': return(Char()); case '(': current += 1; return(Token(TK.LParen)); case ')': current += 1; return(Token(TK.RParen)); case '[': current += 1; return(Token(TK.LSquare)); case ']': current += 1; return(Token(TK.RSquare)); case '{': current += 1; return(Token(TK.LBrace)); case '}': current += 1; return(Token(TK.RBrace)); case ',': current += 1; return(Token(TK.Comma)); case '~': current += 1; return(Token(TK.Tilde)); case ':': current += 1; return(Token(TK.Colon)); case ';': current += 1; return(Token(TK.Semi)); case '?': current += 1; return(Token(TK.Question)); case '|': current += 1; return(Token(TK.Pipe)); case '^': current += 1; return(Token(TK.Hat)); case '&': current += 1; return(Token(TK.Amper)); case '.': current += 1; if (current < limit) { if (Current == '>') { current += 1; return(Token(TK.Chain)); } if (Current == '.' && current + 1 < limit && Next == '.') { current += 2; return(Token(TK.Ellipsis)); } } return(Token(TK.Dot)); case '@': current += 1; if (current < limit && Current == '{') { current += 1; return(Token(TK.AtLBrace)); } return(Token(TK.At)); case '+': current += 1; return(Token(TK.Plus)); case '-': current += 1; if (current < limit) { if (Current == '>') { current += 1; return(Token(TK.Arrow)); } } return(Token(TK.Minus)); case '/': current += 1; return(Token(TK.Divide)); case '*': current += 1; return(Token(TK.Multiply)); case '%': current += 1; if (current < limit) { if (Current == '%') { current += 1; return(Token(TK.Mod)); } } return(Token(TK.Rem)); case '=': current += 1; if (current < limit) { if (Current == '>') { current += 1; return(Token(TK.DblArrow)); } else if (Current == '=') { current += 1; return(Token(TK.Eq)); } } return(Token(TK.Assign)); case '!': current += 1; if (current < limit) { if (Current == '=') { current += 1; return(Token(TK.Ne)); } } return(Token(TK.Exclamation)); case '<': current += 1; if (current < limit) { if (Current == ':') { current += 1; return(Token(TK.Subtype)); } if (Current == '=') { current += 1; return(Token(TK.Le)); } if (Current == '<') { current += 1; return(Token(TK.LShift)); } } return(Token(TK.Lt)); case '>': current += 1; if (current < limit) { if (Current == '=') { current += 1; return(Token(TK.Ge)); } } return(Token(TK.Gt)); default: if (Current.IsLetterOrUnderscore()) { return(IdentifierOrKeyword()); } if (Current.IsDigit()) { return(Number()); } break; } Errors.AtOffset(ErrNo.Lex001, Source, current, $"unknown character ``{CharRep.InText(Current)}´´ in source stream"); current += 1; nl = Skip() || nl; } }
public override string ToString() { return($"'{CharRep.InText(Characters)}'"); }
public override string ToString() { return($"'{CharRep.Convert(Min)}-{CharRep.Convert(Max)}'"); }