Beispiel #1
0
 internal void Init(CSentense sentense, ILogger inLoger)
 {
     _sentense = sentense;
     BuildTokens(sentense, inLoger);
     FindHeadAndTail(inLoger);
     FindCommandParams(inLoger);
 }
Beispiel #2
0
        public IKey Parse(string inFileName, string inText, ILogPrinter inLogger, object inContextData)
        {
            if (!string.IsNullOrEmpty(inFileName))
            {
                _parsed.RemoveAll(p => string.Equals(p.FileName, inFileName));
            }

            var supporter = new CSupportOwner(this, inLogger, inContextData, inFileName);

            _sentenser.ParseText(inText, supporter.GetLogger());

            List <CTokenLine> lines = new List <CTokenLine>();

            for (int i = 0; i < _sentenser.SentenseCount; i++)
            {
                CSentense  sentense = _sentenser[i];
                CTokenLine tl       = new CTokenLine();
                tl.Init(sentense, supporter.GetLogger());
                lines.Add(tl);
            }

            string root_name = Path.GetFileNameWithoutExtension(inFileName);
            CKey   root      = CTreeBuilder.Build(root_name, lines, supporter);

            _parsed.Add(new CParsed(root, lines, inFileName));

            return(root);
        }
Beispiel #3
0
        void BuildTokens(CSentense sentense, ILogger inLoger)
        {
            CToken[] tokens = CTokenFinder.Instance.GetTokens(sentense);

            if (tokens.Length > 0 && tokens[tokens.Length - 1].TokenType == ETokenType.Comment)
            {
                _tokens = new CToken[tokens.Length - 1];
                Array.Copy(tokens, _tokens, _tokens.Length);
                _comments = tokens[tokens.Length - 1];
            }
            else
            {
                _tokens = tokens;
            }

            _error_count += Check(inLoger);
        }
Beispiel #4
0
        Tuple <CToken, int> FindComments(CSentense inSentense)
        {
            if (string.IsNullOrEmpty(inSentense.Text))
            {
                return(new Tuple <CToken, int>(null, 0));
            }

            CToken comment  = null;
            string comm_str = GetTokenString(ETokenType.Comment);
            int    pos      = inSentense.Text.IndexOf(comm_str);

            if (pos == -1)
            {
                return(new Tuple <CToken, int>(null, 0));
            }

            string scomm = inSentense.Text.Substring(pos + comm_str.Length);

            comment = new CToken(ETokenType.Comment, scomm, inSentense.LineNumber, pos + inSentense.Rank * TAB_LENGTH);

            return(new Tuple <CToken, int>(comment, pos));
        }
Beispiel #5
0
        public CToken[] GetTokens(CSentense inSentense)
        {
            if (string.IsNullOrEmpty(inSentense.Text))
            {
                return(new CToken[0]);
            }

            string line = inSentense.Text;
            Tuple <CToken, int> comment_pos = FindComments(inSentense);
            CToken comment = comment_pos.Item1;

            if (comment != null)
            {
                line = line.Substring(0, comment_pos.Item2).TrimEnd(' ').TrimEnd('\t');
            }

            List <CToken> lst = new List <CToken>();

            int tab_shift = inSentense.Rank * TAB_LENGTH;
            int lnum      = inSentense.LineNumber;

            int  world_pos = 0;
            int  world_len = 0;
            int  i         = 0;
            bool open_qute = false;

            while (i < line.Length)
            {
                bool quote = line[i] == '"';
                if (quote)
                {
                    open_qute = !open_qute;
                }

                bool           space = false;
                ITokenTemplate tmp   = null;
                if (!open_qute)
                {
                    space = line[i] == ' ';
                    tmp   = _templates.Find(tt => tt.CheckPassed(line, i));
                }

                bool world_break = space || quote || tmp != null;
                if (world_break)
                {
                    world_len = i - world_pos;
                    if (world_len > 0)
                    {
                        AddWorld(i, world_pos, line, lst, lnum, tab_shift);
                    }

                    if (tmp == null)
                    {
                        world_pos = i + 1;
                    }
                    else
                    {
                        world_pos = i + tmp.GetText().Length;
                    }
                }

                if (tmp == null)
                {
                    i++;
                }
                else
                {
                    lst.Add(new CToken(tmp.GetTokenType(), tmp.GetText(), inSentense.LineNumber, i + inSentense.Rank * TAB_LENGTH));
                    i += tmp.GetText().Length;
                }
            }

            world_len = i - world_pos;
            if (world_len > 0)
            {
                AddWorld(i, world_pos, line, lst, lnum, tab_shift);
            }

            if (comment != null)
            {
                lst.Add(comment);
            }

            return(lst.ToArray());
        }