SourceLocation NextSourceLocation(SourceLocation location, string ch) { if(ch == "\n") { return new SourceLocation(location.File, location.Line + 1, position: 1); } else { return new SourceLocation(location.File, location.Line, location.Position + 1); } }
FSharpOption<MatchResult> MatchOperator_Internal(InputData data, string operStr, SourceLocation sourceLocation) { int numMatches = matchOperatorCreators.Keys.Count(op => op.StartsWith(operStr)); var hasExact = matchOperatorCreators.ContainsKey(operStr); if(numMatches == 1 && hasExact) { return MatchToken(matchOperatorCreators[operStr](sourceLocation), data); } else if(numMatches > 1) { if(data.IsCons) { var resultOpt = MatchOperator_Internal(data.Tail, operStr + data.Head.Value, sourceLocation); ; if(OptionModule.IsSome(resultOpt)) { return resultOpt; } } if(!hasExact) { throw new SyntaxException(); } return MatchToken(matchOperatorCreators[operStr](sourceLocation), data); } else { return FSharpOption<MatchResult>.None; } }
static Token MatchIdentifier_CreateToken(string id, SourceLocation sourceLocation) { switch(id) { case "def": return new Token.KW_DEF(sourceLocation); case "proc": return new Token.KW_PROC(sourceLocation); case "initialize": return new Token.KW_INITIALIZE(sourceLocation); case "construct": return new Token.KW_CONSTRUCT(sourceLocation); case "do": return new Token.KW_DO(sourceLocation); case "end": return new Token.KW_END(sourceLocation); case "var": return new Token.KW_VAR(sourceLocation); case "val": return new Token.KW_VAL(sourceLocation); case "true": return new Token.KW_TRUE(sourceLocation); case "false": return new Token.KW_FALSE(sourceLocation); case "as": return new Token.KW_AS(sourceLocation); case "namespace": return new Token.KW_NAMESPACE(sourceLocation); case "using": return new Token.KW_USING(sourceLocation); case "class": return new Token.KW_CLASS(sourceLocation); case "public": return new Token.KW_PUBLIC(sourceLocation); case "protected": return new Token.KW_PROTECTED(sourceLocation); case "private": return new Token.KW_PRIVATE(sourceLocation); case "internal": return new Token.KW_INTERNAL(sourceLocation); case "base": return new Token.KW_BASE(sourceLocation); case "if": return new Token.KW_IF(sourceLocation); case "then": return new Token.KW_THEN(sourceLocation); case "else": return new Token.KW_ELSE(sourceLocation); case "elsif": return new Token.KW_ELSIF(sourceLocation); case "extendable": return new Token.KW_EXTENDABLE(sourceLocation); case "sealed": return new Token.KW_SEALED(sourceLocation); case "virtual": return new Token.KW_VIRTUAL(sourceLocation); case "abstract": return new Token.KW_ABSTRACT(sourceLocation); case "override": return new Token.KW_OVERRIDE(sourceLocation); case "final": return new Token.KW_FINAL(sourceLocation); case "trait": return new Token.KW_TRAIT(sourceLocation); case "andThen": return new Token.KW_ANDTHEN(sourceLocation); case "_": return new Token.KW_UNDERSCORE(sourceLocation); default: return new Token.Identifier(id, sourceLocation); } }
IEnumerable<WithSource<string>> GetCharactersWithSourceLocation(IEnumerable<string> graphemes, string inputFileName) { var sourceLocation = new SourceLocation(inputFileName, 1, 1); foreach(var grapheme in graphemes) { yield return new WithSource<string>(grapheme, sourceLocation); sourceLocation = NextSourceLocation(sourceLocation, grapheme); } }