示例#1
0
        public static List <LogicalLine> getLines(string fileContent, string fileName = "<unknown file>")
        {
            var result = new List <LogicalLine>();

            var fileLines = fileContent.Split(new char[] { '\n', '\r' });

            int  fileLineNumber      = 1;
            bool canHaveFollowOnLine = false;

            LogicalLine logicalLine = new LogicalLine();

            foreach (string fileLine in fileLines)
            {
                string trimmedFileLine = fileLine.Trim();
                if (trimmedFileLine.Length == 0)
                {
                    canHaveFollowOnLine = false;
                }
                else if (trimmedFileLine[0] == '#')
                {
                    // Ignore comments.
                }
                else if (char.IsWhiteSpace(fileLine[0]))
                {
                    // We have a follow-on line, make sure we follow on from something.
                    if (!canHaveFollowOnLine)
                    {
                        string msg = "Can't have a follow-on line at the start, or after a blank line.";
                        throw new gnoam.file.SyntaxError(msg, fileName, fileLineNumber);
                    }

                    // Add the follow on line to the buffer.
                    logicalLine.AddNewLine(fileLine, fileLineNumber);
                }
                else
                {
                    // We have a new line, so clear the buffer.
                    if (!logicalLine.IsEmpty)
                    {
                        result.Add(logicalLine);
                        logicalLine = new LogicalLine();
                    }

                    // Add this first part of the line to the buffer.
                    logicalLine.AddNewLine(fileLine, fileLineNumber);
                    canHaveFollowOnLine = true;
                }
                ++fileLineNumber;
            }
            // Add any remaining content.
            if (!logicalLine.IsEmpty)
            {
                result.Add(logicalLine);
            }

            return(result);
        }
示例#2
0
        public static List<LogicalLine> getLines(string fileContent, string fileName="<unknown file>")
        {
            var result = new List<LogicalLine>();

              var fileLines = fileContent.Split(new char[] {'\n','\r'});

              int fileLineNumber = 1;
              bool canHaveFollowOnLine = false;

              LogicalLine logicalLine = new LogicalLine();

              foreach (string fileLine in fileLines) {
            string trimmedFileLine = fileLine.Trim();
            if (trimmedFileLine.Length == 0) {
              canHaveFollowOnLine = false;
            } else if (trimmedFileLine[0] == '#') {
              // Ignore comments.
            } else if (char.IsWhiteSpace(fileLine[0])) {
              // We have a follow-on line, make sure we follow on from something.
              if (!canHaveFollowOnLine) {
            string msg = "Can't have a follow-on line at the start, or after a blank line.";
            throw new gnoam.file.SyntaxError(msg, fileName, fileLineNumber);
              }

              // Add the follow on line to the buffer.
              logicalLine.AddNewLine(fileLine, fileLineNumber);
            } else {
              // We have a new line, so clear the buffer.
              if (!logicalLine.IsEmpty) {
            result.Add(logicalLine);
            logicalLine = new LogicalLine();
              }

              // Add this first part of the line to the buffer.
              logicalLine.AddNewLine(fileLine, fileLineNumber);
              canHaveFollowOnLine = true;
            }
            ++fileLineNumber;
              }
              // Add any remaining content.
              if (!logicalLine.IsEmpty) result.Add(logicalLine);

              return result;
        }