public SlotModel(ParserRule rule, string baseRule, string baseNamespace, GrammarInfo grammarInfo) : base(rule: rule, baseNamespace: baseNamespace) { if (rule.Alternatives.Count != 1) { throw new ArgumentException(); } BaseRuleName = baseRule?.ToPascalcase(); SyntaxKind = $"{rule.Name.ToPascalcase()}Syntax"; int index = 0; if (IsSection) { // Überprüfen, ob es sich um eine benannte Sektion handelt var expectedSectionName = RuleName.Replace("Section", "Identifier"); // Die Begin-/End Sektionen muss es geben, andernfalls ist die Grammatik falsch formuliert var sb = grammarInfo.Rules.Single(r => r.Name?.ToPascalcase() == SectionBeginName); var sbName = sb.Alternatives.Single().Elements.FirstOrDefault(e => e.Name.ToPascalcase() == expectedSectionName); var se = grammarInfo.Rules.Single(r => r.Name?.ToPascalcase() == SectionEndName); var seName = se.Alternatives.Single().Elements.FirstOrDefault(e => e.Name.ToPascalcase() == expectedSectionName); if (sbName != null && seName != null) { IsNamedSection = true; NamedSectionIdentifierName = expectedSectionName; } } foreach (var element in rule.Alternatives.Single().Elements) { var name = element.Name.ToPascalcase(); if (name == "EOF") { name = "Eof"; } var syntaxKind = element.SyntaxKind; if (syntaxKind == "EOF") { syntaxKind = "Eof"; } Slots.Add(new SlotMemberModel { Name = name, IsToken = element is TokenElement, IsLabeled = element.IsLabeled, SyntaxKind = syntaxKind, Cardinality = element.Cardinality, SlotIndex = index++ }); } }
public string JavaScript() { System.Text.StringBuilder bldr = new System.Text.StringBuilder(); bldr.Append("function(value) {if ("); bldr.Append(RuleName.Replace(" ", "_")); bldr.Append("(value"); if (AllowedValue != null) { bldr.Append(string.Format(", {0}))", AllowedValueString())); } else { bldr.Append("))"); } bldr.Append(" return "); bldr.Append(Description.Surround("'")); bldr.Append("; }"); return(bldr.ToString()); }