private String PreflightCheck(MatchingDefinition definition, List <ScopeItem> scopeItems, bool dryRun) { string errorMessage = null; int placeholderIndex = 0; foreach (Placeholder placeholder in definition.Placeholders) { string value = definition.GetPlaceholderValue(placeholderIndex); if (placeholder.IsAlias) { // trying to create 'value' - check it doesn't already exist ScopeItem existingItem = FindScopeItemByName(scopeItems, value); if (existingItem != null) { errorMessage = $"Already exists: {value}"; } else if (!dryRun) { scopeItems.Add(new ScopeItem(value, placeholder.TypeName)); } } placeholderIndex++; } return(errorMessage); }
private List <MatchingDefinition> FindMatchingDefinitions(string input) { List <MatchingDefinition> result = new List <MatchingDefinition>(); foreach (LineDefinition lineDefinition in lineDefinitions) { MatchingDefinition match = TestMatch(input, lineDefinition); if (match != null) { result.Add(match); } } return(result); }
public DSLTranslation Process(string input) { DSLTranslation result = new DSLTranslation(); List <ScopeItem> scopeItems = new List <ScopeItem>(); List <MatchingDefinition> lineMatches = new List <MatchingDefinition>(); foreach (string rawLine in input.Split('\n', StringSplitOptions.RemoveEmptyEntries)) { string line = rawLine.Trim(); if (line.Length > 0) { MatchingDefinition match = ProcessLine(line, result, scopeItems); lineMatches.Add(match); } } // render step foreach (MatchingDefinition lineMatch in lineMatches) { lineMatch.RenderInto(result); } return(result); }
private MatchingDefinition TestMatch(string input, LineDefinition lineDefinition) { MatchingDefinition result = new MatchingDefinition(lineDefinition); int inputIndex = 0; foreach (IMatchingStep matchingStep in lineDefinition.MatchingSteps) { if (matchingStep.GetType() == typeof(ScaffoldMatchingStep)) { ScaffoldMatchingStep scaffold = (ScaffoldMatchingStep)matchingStep; if ((inputIndex + scaffold.ScaffoldText.Length) > input.Length) { return(null); // template is longer than input - so no match. } var segment = input.Substring(inputIndex, scaffold.ScaffoldText.Length); if (segment.Equals(scaffold.ScaffoldText)) { inputIndex += segment.Length; } else { return(null); // No match. } } else // its a placeholder { Placeholder placeholder = (Placeholder)matchingStep; if (placeholder.IsAlias || placeholder.IsReference) { int candidateNextPosition = GetPlaceholderFrom(input, inputIndex); if (candidateNextPosition == -1) { // full placeholder not found return(null); // No match. } string placeholderContent = input.Substring(inputIndex + 1, (candidateNextPosition - inputIndex) - 1); result.AddPlaceholderValue(placeholderContent); inputIndex = candidateNextPosition + 1; } else if (placeholder.IsLiteral) { if (placeholder.TypeName.Equals("number")) { string number = GetNumberFrom(input, inputIndex); if (number == null) { return(null); } result.AddPlaceholderValue(number); inputIndex += number.Length; } else { throw new NotSupportedException($"Unhandled literal type: {placeholder.TypeName}"); } } else { throw new NotSupportedException(); } } } if (inputIndex != input.Length) { return(null); //Somethings gone wrong. } return(result); }