public static Parse <string> BoolStringHelper(string boolValue) { return(value => { ParseResult <char> whiteSpaceResult = IsWhitespace()(value); if (!whiteSpaceResult.Succeeded) { ParseResult <char> parseResult = LetterOrDigit()(value); if (parseResult.Succeeded) { var stringVal = StringValue(boolValue + parseResult.Result.ToString()); ParseResult <string> result = stringVal(parseResult.RemainingInput); if (result.Result.ToLower() == "true") { return new ParseResult <string>("true", parseResult.RemainingInput, true); } else { return new ParseResult <string>("false", parseResult.RemainingInput, true); } } if (string.IsNullOrEmpty(boolValue)) { return new ParseResult <string>("false", value, false); } return new ParseResult <string>("false", value, true); } else { var stringVal = StringValue(boolValue); ParseResult <string> result = stringVal(whiteSpaceResult.RemainingInput); if (result.Result.ToLower() == "true") { return new ParseResult <string>("true", result.RemainingInput, true); } else { return new ParseResult <string>("false", result.RemainingInput, true); } } }); }
private void InitStructures() { Path = from a in Letter from b in LetterOrDigit.Or(Dot).ManyString() select string.Concat(a, b); Name = from a in Letter from b in LetterOrDigit.ManyString() select string.Concat(a, b); String = AnyCharExcept('"').ManyString().Between(Quote); }
/// <summary> /// Creates and initializes a new <see cref="MicrocodeCompiler"/>. /// </summary> public MicrocodeCompiler() { Aliases = new MicrocodeAlias[] { }; KnownInputModes = new char[] { '$', '#', '?' }; CommentHead = String("//"); Operator = OneOf(Aliases.Select(o => Try(String(o.Alias)).ThenReturn(o))); ExceptEndLine = AnyCharExcept('\r', '\n'); Comment = CommentHead.Then(ExceptEndLine.SkipMany()).Labelled("comment"); Documentation = from name in LetterOrDigit.AtLeastOnceString().Labelled("name") from inputs in OneOf(KnownInputModes).Labelled("input-mode").Many().Labelled("input-modes") from colon in Char(':').Before(SkipWhitespaces) from desc in ExceptEndLine.ManyString().Labelled("description") select new MicrocodeDoc() { CommandName = name, InputModes = inputs.Select(i => i.GetValueType()).ToArray(), Description = desc }; SignalCall = OneOf( Operator .Select(o => new MicrocodeCall(o.OperationName)) .Labelled("control alias"), Letter.AtLeastOnceString() .Select(s => new MicrocodeCall(s)) .Labelled("control signal")); Command = from n in Documentation from lb in EndOfLine from cs in Char(' ').SkipMany().Then(SignalCall.Or(Comment.ThenReturn(new MicrocodeCall()))).SeparatedAndOptionallyTerminatedAtLeastOnce(EndOfLine) select new MicrocodeCommand() { Documentation = n, Calls = cs }; Microcode = Comment.ThenReturn(new Maybe <MicrocodeCommand>()).Or( Command.Select(c => new Maybe <MicrocodeCommand>(c))) .SeparatedAtLeastOnce(SkipWhitespaces) .Select(c => c.Where(m => m.HasValue).Select(m => m.Value)); }
/* * constraint := tagfilter | semverfilter | branch | tag | revision | and | or * * tagfilter := [\w]+ * semverfilter := '^' semver | '~' semver | ('>=' | '<=' | '>' | '<') semver | semver '-' semver * branch := 'branch=' [\w]+ * tag := 'tag=' [\w]+ * revision := 'revision=' [\w]+ * and := 'and(' constraint* ')' * or := 'or(' constraint* ')' */ static VersionConstraintParser() { Parser <char, VersionConstraint> constraint = null; Parser <char, VersionConstraint> and = null; Parser <char, VersionConstraint> or = null; Parser <char, string> identifier = Trim(LetterOrDigit.ManyString()); Parser <char, string> digits = Digit.ManyString(); Parser <char, Semver> version = Map( (major, _, minor, __, patch) => new Semver(int.Parse(major), int.Parse(minor), int.Parse(patch)), digits, Char('.'), digits, Char('.'), digits ); Parser <char, VersionConstraint> literal = identifier.Map((tag) => (VersionConstraint) new TagConstraint(tag)); Parser <char, ISemverComparison> greater = Char('^').Then(version).Map((semver) => (ISemverComparison) new GreaterEqualComparison(semver)); Parser <char, ISemverComparison> plusminor = Char('~').Then(version).Map((semver) => (ISemverComparison) new GreaterMinorRevisionComparison(semver)); Parser <char, ISemverComparison> ge = String(">=").Then(version).Map((semver) => (ISemverComparison) new GreaterEqualComparison(semver)); Parser <char, ISemverComparison> le = String("<=").Then(version).Map((semver) => (ISemverComparison) new LessEqualComparison(semver)); Parser <char, ISemverComparison> g = Char('>').Then(version).Map((semver) => (ISemverComparison) new GreaterComparison(semver)); Parser <char, ISemverComparison> l = Char('<').Then(version).Map((semver) => (ISemverComparison) new LessComparison(semver)); Parser <char, ISemverComparison> between = Map((start, _, end) => (ISemverComparison) new InRangeComparison(start, end), Trim(version), Trim(Char('-')), Trim(version)); Parser <char, VersionConstraint> semver = greater.Or(plusminor).Or(ge).Or(le).Or(g).Or(l).Or(between).Map((comp) => (VersionConstraint) new SemverConstraint(comp)); Parser <char, VersionConstraint> branch = Trim(String("branch")).Then(Trim(Char('='))).Then(identifier).Map((str) => (VersionConstraint) new BranchConstraint(str)); Parser <char, VersionConstraint> tag = Trim(String("tag")).Then(Trim(Char('='))).Then(identifier).Map((str) => (VersionConstraint) new TagConstraint(str)); Parser <char, VersionConstraint> revision = Trim(String("revision")).Then(Trim(Char('='))).Then(identifier).Map((str) => (VersionConstraint) new TagConstraint(str)); and = Trim(String("and")).Then(Trim(Char('('))).Then(Rec(() => constraint)).Many().Before(Trim(Char(')'))).Map(constraints => (VersionConstraint) new AndConstraint(constraints)); or = Trim(String("or")).Then(Trim(Char('('))).Then(Rec(() => constraint)).Many().Before(Trim(Char(')'))).Map(constraints => (VersionConstraint) new OrConstraint(constraints)); constraint = and.Or(or).Or(revision).Or(tag).Or(branch).Or(semver).Or(literal); DSLparser = constraint; }
private static Parser <char, string> HexString(int length) => LetterOrDigit .RepeatString(length);
/// <summary> /// Creates and initializes a new <see cref="CommandCompiler"/>. /// </summary> public CommandCompiler() { CommentHead = String("//"); LiteralMarker = Char('#'); CharMarker = Char('\''); StringMarker = Char('\"'); AddressMarker = Char('$'); DirectiveMarker = Char('.'); ReferenceMarker = Char('?'); AnyCommandMarker = OneOf(LiteralMarker, AddressMarker, ReferenceMarker); EnumStart = Char('{'); EnumEnd = Char('}'); ExceptEndLine = AnyCharExcept('\r', '\n'); TrueString = String("true"); FalseString = String("false"); NullString = String("null"); ReservedCommandNames = new string[] { "Define", "Var", "Constant" }; NumLiteral = from l in CurrentPos from n in LiteralMarker.Then(Digit.Or(Char('.')).Or(Char('-')).ManyString()) select new ValueToken() { Value = n, Type = ValueType.Immediate, Position = l }; CharLiteral = from l in CurrentPos from c in Any.Between(CharMarker) select new ValueToken() { Value = c.ToString(), Type = ValueType.Immediate, Position = l }; StringLiteral = from l in CurrentPos from c in AnyCharExcept('\"').ManyString().Between(StringMarker) select new ValueToken() { Value = c.ToString(), Type = ValueType.Immediate, Position = l }; BoolLiteral = from l in CurrentPos from b in OneOf(Try(TrueString), FalseString) select new ValueToken() { Value = b, Type = ValueType.Immediate, Position = l }; EnumLiteral = from l in CurrentPos from e in LetterOrDigit.ManyString().Between(EnumStart, EnumEnd) select new ValueToken() { Value = e, Type = ValueType.Immediate, Position = l }; NullLiteral = from l in CurrentPos from n in NullString select new ValueToken() { Value = string.Empty, Type = ValueType.Immediate, Position = l }; Address = from l in CurrentPos from a in AddressMarker from n in Num select new ValueToken() { Value = n.ToString(), Type = ValueType.Address, Position = l }; Directive = from l in CurrentPos from d in DirectiveMarker from n in LetterOrDigit.Or(DirectiveMarker).ManyString() select new ValueToken() { Value = n, Type = ValueType.Directive, Position = l }; Reference = from l in CurrentPos from r in ReferenceMarker from n in LetterOrDigit.Or(DirectiveMarker).ManyString() select new ValueToken() { Value = n, Type = ValueType.Reference, Position = l }; Value = OneOf( NumLiteral.Labelled("number"), CharLiteral.Labelled("char"), StringLiteral.Labelled("string"), EnumLiteral.Labelled("enum"), Try(BoolLiteral).Labelled("bool"), Try(NullLiteral).Labelled("null"), Address.Labelled("address"), Directive.Labelled("directive"), Reference.Labelled("reference")); CompilerCommand = from l in CurrentPos from c in LetterOrDigit.AtLeastOnceString().Where(n => ReservedCommandNames.Contains(n)).Labelled("reserved command") from v in Try(SkipWhitespaces.Then(Value)).Many() select new CommandCall() { Name = c, Inputs = v.ToArray(), Type = CallType.Compiler, Position = l }; CallCommand = from l in CurrentPos from c in LetterOrDigit.Or(AnyCommandMarker).AtLeastOnceString().Where(n => KnownCommandNames.Contains(n)).Labelled("known command") from v in Try(SkipWhitespaces.Then(Value)).Many() select new CommandCall() { Name = c, Inputs = v.ToArray(), Type = CallType.Command, Position = l }; Command = OneOf(Try(CompilerCommand).Labelled("compiler call"), CallCommand.Labelled("command call")); Code = OneOf( Try(CommentHead.Then(ExceptEndLine.SkipMany())).ThenReturn <CommandCall?>(null).Labelled("comment"), Command.Select <CommandCall?>(c => c)) .SeparatedAndOptionallyTerminatedAtLeastOnce(SkipWhitespaces); }