示例#1
0
        public void LoadLineDefinitionFromFile(string fullFilename)
        {
            string filename       = Path.GetFileName(fullFilename);
            int    extensionIndex = filename.LastIndexOf('.');
            string lineTemplate   = Formatting.SimplifySpacing(filename.Substring(0, extensionIndex));

            IList <OutputItem> outputItems = new List <OutputItem>();
            LineDefinition     definition  = new LineDefinition(lineTemplate, outputItems);

            int lineNumber = 0;

            foreach (string rawLine in File.ReadAllLines(fullFilename))
            {
                string line = rawLine.Trim();
                if (line.Length > 0)
                {
                    int separatorIndex = line.IndexOf(':');
                    if (separatorIndex == -1)
                    {
                        throw new DSLTemplateException($"Output line:{lineNumber} of {lineTemplate} has no ':' separator");
                    }

                    string     outputCollection = line.Substring(0, separatorIndex).Trim();
                    string     outputTemplate   = line.Substring(separatorIndex + 1).Trim();
                    OutputItem outputItem       = new OutputItem(definition, lineNumber, outputCollection, outputTemplate);

                    outputItems.Add(outputItem);
                }
                lineNumber++;
            }

            lineDefinitions.Add(definition);
        }
示例#2
0
 public OutputItem(
     LineDefinition lineDefinition,
     int sourceTemplateLineNumber,
     string outputCollection,
     string outputPattern)
 {
     this.lineDefinition           = lineDefinition;
     this.sourceTemplateLineNumber = sourceTemplateLineNumber;
     this.outputCollection         = outputCollection;
     this.outputPattern            = outputPattern;
 }
 public MatchingDefinition(LineDefinition lineDefinition)
 {
     this.lineDefinition = lineDefinition;
 }
示例#4
0
        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);
        }