parsercommon.cs

来自「csv解析类」· CS 代码 · 共 112 行

CS
112
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace QiHe.CodeLib.Csv
{
    public partial class Parser
    {
        int position;
        ParserInput<Char> Input;

        public List<string> Errors = new List<string>();

        public Parser() { }

        private void SetInput(ParserInput<Char> input)
        {
            Input = input;
            position = 0;
        }

        private char MatchTerminal(char ch, out bool success)
        {
            success = false;
            if (Input.HasInput(position))
            {
                Char symbol = Input.GetInputSymbol(position);
                if (ch == symbol)
                {
                    position++;
                    success = true;
                }
                return symbol;
            }
            return default(Char);
        }

        private char MatchTerminalRange(char start, char end, out bool success)
        {
            success = false;
            if (Input.HasInput(position))
            {
                Char symbol = Input.GetInputSymbol(position);
                if (start <= symbol && symbol <= end)
                {
                    position++;
                    success = true;
                }
                return symbol;
            }
            return default(Char);
        }

        private char MatchTerminalSet(string terminalSet, bool isComplement, out bool success)
        {
            success = false;
            if (Input.HasInput(position))
            {
                Char symbol = Input.GetInputSymbol(position);
                bool match = isComplement ? terminalSet.IndexOf(symbol) == -1 : terminalSet.IndexOf(symbol) > -1;
                if (match)
                {
                    position++;
                    success = true;
                }
                return symbol;
            }
            return default(Char);
        }

        private bool MatchTerminal(char ch)
        {
            if (Input.HasInput(position))
            {
                Char symbol = Input.GetInputSymbol(position);
                if (ch == symbol)
                {
                    position++;
                    return true;
                }
            }
            return false;
        }

        private string MatchTerminalString(string terminalString, out bool success)
        {
            int currrent_position = position;
            foreach (char ch in terminalString)
            {
                if (!MatchTerminal(ch))
                {
                    position = currrent_position;
                    success = false;
                    return null;
                }
            }
            success = true;
            return terminalString;
        }

        private void Error(string message)
        {
            Errors.Add(Input.LocateError(position, message));
        }

        private void ClearError()
        {
            Errors.Clear();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?