예제 #1
0
 public LexObject(int lineNum, int charNum, LexType type, string value = "")
 {
     LineNumber      = lineNum;
     CharacterNumber = charNum;
     Type            = type;
     Value           = value;
 }
예제 #2
0
파일: Output.cs 프로젝트: Snappey/Tracker
 public Output(FolderManager manager)
 {
     _manager    = manager;
     _lexFlags   = (LexType.Command | LexType.Function | LexType.Hook | LexType.HookCall);
     _outputFlag = OutputFlag.Replicate;
     _path       = Directory.GetCurrentDirectory() + "\\" + _manager.GetName();
 }
예제 #3
0
 private Lexema LexChar(LexType type)
 {
     StartLexema();
     AddChar();
     SetLexType(type);
     return(EndLexema());
 }
예제 #4
0
 private void AddRes(string text, LexType tp, int col, int row)
 {
     res.Add(new Lexem()
     {
         LexemText = text, LexemType = tp, ColNum = col, RowNum = row
     });
     status = LexType.None;
 }
예제 #5
0
 private static void Expect(IEnumerator <LexObject> iterator, LexType lexType)
 {
     iterator.MoveNext();
     if (iterator.Current.Type != lexType)
     {
         throw new ProviderException("Unable to parse FGD. Expected " + lexType + ", got " + iterator.Current.Type + ".\n" +
                                     "On line " + iterator.Current.LineNumber + ", character " + iterator.Current.CharacterNumber);
     }
 }
예제 #6
0
        // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
        private Lexema L(LexType type)
        {
            var l = sc.Next();

            if (l.Type != type)
            {
                throw new ParseException(l, type);
            }
            return(l);
        }
예제 #7
0
 private void ReOpenString()
 {
     cur       = new StringBuilder();
     curColNum = res.Last().ColNum;
     curRowNum = res.Last().RowNum;
     cur.Append(InsertSpecChars(res.Last().LexemText, res.Last().LexemType));
     TextQuote = ' ';
     res.RemoveAt(res.Count - 1);
     status = LexType.Command;
 }
예제 #8
0
 private string InsertSpecChars(string text, LexType tp)
 {
     if (tp == LexType.Text || tp == LexType.Command)
     {
         return(text.Replace("\r", "\\r").Replace("\n", "\\n"));
     }
     else
     {
         return(text);
     }
 }
예제 #9
0
        private void PushString()
        {
            if (status == LexType.None)
            {
                return;
            }
            string s = cur.ToString();

            if (s == "")
            {
                status = LexType.None; return;
            }
            if (status == LexType.Command)
            {
                s = s.Trim();
                if (s.Last() == '.')
                {
                    throw new Exception("Dot in position" + i.ToString());
                }
            }

            if (status == LexType.Text)
            {
                if (s.Length < 2)
                {
                    throw new Exception("Not closed string in position " + i.ToString());
                }
                if (TextQuote == '[')
                {
                    if (s[s.Length - 1] != ']')
                    {
                        throw new Exception("Not closed string in position " + i.ToString());
                    }
                }
                else
                if (TextQuote != s[s.Length - 1])
                {
                    throw new Exception("Not closed string in position " + i.ToString());
                }
            }
            //s = RemoveSpecChars(s, status);
            AddRes(s, status, curColNum, curRowNum);
            TextQuote = ' ';
            cur       = new StringBuilder();
            curColNum = colNum;
            curRowNum = rowNum;
        }
예제 #10
0
        private string RemoveSpecChars(string text, LexType tp)
        {
            string text2 = text;

            if (tp == LexType.Text || tp == LexType.Command)
            {
                StringBuilder sb       = new StringBuilder();
                bool          WasSlash = false;//был ли предыдущий символ слешем
                for (int i = 0; i < text.Length; i++)
                {
                    char c = text[i];
                    if (c == '\\')
                    {
                        if (WasSlash)
                        {
                            sb.Append(c);
                            WasSlash = false;
                            continue;
                        }
                        WasSlash = true;
                        continue;
                    }
                    if (WasSlash && c == 'r')
                    {
                        sb.Append("\r");
                    }
                    else if (WasSlash && c == 'n')
                    {
                        sb.Append("\n");
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    WasSlash = false;
                }
                if (WasSlash)
                {
                    sb.Append('\\');
                }
                text2 = sb.ToString();
            }
            return(text2);
        }
예제 #11
0
        public static Lex Parse(string value, LexType lexType)
        {
            if (_currentLex >= _lexes.Count)
            {
                throw new Exception("Lexes is ended up");
            }

            var token = _lexes[_currentLex];

            if ((value == null || token.Value == value) && (lexType == null || token.LexType == lexType))
            {
                _currentLex++;
                return(token);
            }
            else
            {
                throw new Exception("Error");
            }
        }
예제 #12
0
파일: Output.cs 프로젝트: Snappey/Tracker
 public void SetLexFlags(LexType flag)
 {
     _lexFlags = flag;
 }
예제 #13
0
        public static LexToken[] Execute(string Code)
        {
            List <LexToken> Tokens = new List <LexToken>();
            StringBuilder   sb     = new StringBuilder();

            for (int i = 0; i < Code.Length; i++)
            {
                bool    idSwitch = false;
                LexType type     = LexType.Unknown;

                switch (Code[i])
                {
                case '(':
                {
                    type = LexType.RoundBracketOpen;
                    break;
                }

                case ')':
                {
                    type = LexType.RoundBracketClose;
                    break;
                }

                case '"':
                {
                    type = LexType.Quote;
                    break;
                }

                default:
                {
                    sb.Append(Code[i]);
                    idSwitch = true;
                    break;
                }
                }

                if (!idSwitch)
                {
                    if (sb.Length > 0)
                    {
                        Tokens.Add(new LexToken()
                        {
                            Value = sb.ToString(), Type = LexType.Identifier
                        });
                        sb.Clear();
                    }

                    if (sb.Length == 0)
                    {
                        Tokens.Add(new LexToken()
                        {
                            Value = Code[i].ToString(), Type = type
                        });
                    }
                }
            }

            return(Tokens.ToArray());
        }
예제 #14
0
 public static Term Of(this LexType type) => new Term(type);
예제 #15
0
 public Term(LexType type)
 {
     Type = type;
 }
예제 #16
0
파일: Lexer.cs 프로젝트: shurik111333/spbu
 public Lexem(int start, int end)
 {
     LexType = LexType.String;
     Value = 0;
     VarName = expression.Substring(start, end - start + 1);
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
예제 #17
0
 private static void Expect(IEnumerator<LexObject> iterator, LexType lexType)
 {
     iterator.MoveNext();
     if (iterator.Current.Type != lexType)
     {
         throw new ProviderException("Unable to parse FGD. Expected " + lexType + ", got " + iterator.Current.Type + ".");
     }
 }
예제 #18
0
파일: FgdProvider.cs 프로젝트: silky/sledge
 private static void Expect(IEnumerator<LexObject> iterator, LexType lexType)
 {
     iterator.MoveNext();
     if (iterator.Current.Type != lexType)
     {
         throw new ProviderException("Unable to parse FGD. Expected " + lexType + ", got " + iterator.Current.Type + ".\n" +
                                     "On line " + iterator.Current.LineNumber + ", character " + iterator.Current.CharacterNumber);
     }
 }
예제 #19
0
파일: Lexer.cs 프로젝트: shurik111333/spbu
 public Lexem(LexType type)
 {
     LexType = type;
     Value = 0;
     VarName = "";
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
예제 #20
0
파일: Lexer.cs 프로젝트: shurik111333/spbu
 public Lexem(float number)
 {
     LexType = LexType.Number;
     Value = number;
     VarName = "";
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
예제 #21
0
 public Lex(int id, LexType lexType, string value)
 {
     Id      = id;
     LexType = lexType;
     Value   = value;
 }
예제 #22
0
 private void SetLexType(LexType type)
 {
     curr.Type = type;
 }
예제 #23
0
 public LexObject(LexType type, string value = "")
 {
     Type = type;
     Value = value;
 }
예제 #24
0
 public Lexema(LexType lexType, int start, string value)
 {
     this.lexType = lexType;
     this.start = start;
     this.value = value;
 }
예제 #25
0
 public Lexema(LexType lexType, int start)
     : this(lexType, start, null)
 {
 }
예제 #26
0
파일: Lexer.cs 프로젝트: shurik111333/spbu
 public Lexem(string name)
 {
     LexType = LexType.Variable;
     Value = 0;
     VarName = name;
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
예제 #27
0
        /// <summary>
        /// Осуществляет разбор на лексемы
        /// </summary>
        public void LexemParse(string s)
        {
            rowNum        = 0;
            colNum        = 0;
            currentString = s;

            for (i = 0; i < s.Length; i++)
            {
                c = s[i];
                char?nextC = null;
                if (i < s.Length - 1)
                {
                    nextC = s[i + 1];
                }
                colNum++;

                if (status != LexType.Text && (c == '-' && nextC.HasValue && nextC.Value == '-'))
                {
                    PushString();
                    while (c != '\n' && i < s.Length - 1)
                    {
                        i++;
                        colNum++;
                        c = s[i];
                    }
                    continue;
                }
                if (status != LexType.Text && (c == '/' && nextC.HasValue && nextC.Value == '/'))
                {
                    PushString();
                    while (c != '\n' && i < s.Length - 1)
                    {
                        i++;
                        colNum++;
                        c = s[i];
                    }
                    continue;
                }

                if (status != LexType.Text && (c == '/' && nextC.HasValue && nextC.Value == '*'))
                {
                    PushString();
                    while (i < s.Length - 1)
                    {
                        i++;
                        c = s[i];
                        if (i < s.Length - 1)
                        {
                            nextC = s[i + 1];
                        }
                        else
                        {
                            nextC = null;
                        }
                        colNum++;
                        if (c == '\n')
                        {
                            rowNum++;
                            colNum = 0;
                        }
                        if (c == '*' && nextC.HasValue && nextC.Value == '/')
                        {
                            break;
                        }
                    }
                    i++;
                    continue;
                }

                switch (c)
                {
                case ',':
                    if (status == LexType.Text)
                    {
                        cur.Append(c);
                    }
                    else
                    {
                        PushString();
                        AddRes(c.ToString(), LexType.Zpt, colNum, rowNum);
                    }
                    break;

                case ')':
                case '(':
                    if (status == LexType.Text)
                    {
                        cur.Append(c);
                    }
                    else
                    {
                        PushString();
                        AddRes(c.ToString(), LexType.Skobka, colNum, rowNum);
                    }
                    break;

                case '!':
                case '*':
                case '/':
                case '+':
                case '-':
                case '<':
                case '>':
                case '=':
                    if (status == LexType.Text)
                    {
                        cur.Append(c);
                    }
                    else
                    {
                        string curv = cur.ToString();
                        if (c == '*' && status == LexType.Command && curv.Last() == '.')
                        {
                            cur.Append(c);
                        }
                        else
                        if (status == LexType.Arfimetic && ((curv == "<" && c == '>') || (curv == ">" && c == '=') || (curv == "<" && c == '=') || (curv == "!" && c == '=')))
                        {
                            cur.Append(c);
                        }
                        else
                        {
                            PushString();
                            cur.Append(c);
                            status = LexType.Arfimetic;
                        }
                    }
                    break;

                case ' ':
                case '\t':
                case '\r':
                case '\n':
                    if (c == '\n')
                    {
                        rowNum++;
                        colNum = 0;
                    }
                    if (status == LexType.Text)
                    {
                        cur.Append(c);
                    }
                    else
                    {
                        PushString();
                    }
                    break;

                case '\'':
                case '"':
                    if (status != LexType.Text && status != LexType.Command)
                    {
                        PushString(); cur.Append(c); TextQuote = c; status = LexType.Text;
                    }
                    else if (status == LexType.Command)
                    {    //меняем тип
                        if (PrevChar() != '.')
                        {
                            PushString(); cur.Append(c); TextQuote = c; status = LexType.Text;
                        }
                        else
                        {
                            cur.Append(c); TextQuote = c; status = LexType.Text;
                        }
                    }
                    else    //status = LexType.Text
                    {
                        if (c == TextQuote)
                        {
                            if (NextChar() == c)
                            {
                                cur.Append(c);
                                i++; colNum++;
                                continue;
                            }
                            else
                            {
                                cur.Append(c); PushString();
                            }
                        }
                        else
                        {
                            cur.Append(c);
                        }
                    }
                    break;

                case '[':
                    if (status != LexType.Text && status != LexType.Command)
                    {
                        PushString(); cur.Append(c); TextQuote = c; status = LexType.Text;
                    }
                    else if (status == LexType.Command)
                    {    //меняем тип
                        if (PrevChar() != '.')
                        {
                            PushString(); cur.Append(c); TextQuote = c; status = LexType.Text;
                        }
                        else
                        {
                            cur.Append(c);
                            TextQuote = c;
                            status    = LexType.Text;
                        }
                    }
                    else    //status = LexType.Text
                    {
                        cur.Append(c);
                    }
                    break;

                case ']':
                    if (status == LexType.Text)
                    {
                        if (TextQuote == '[')
                        {
                            if (NextChar() == ']')
                            {
                                cur.Append(c);
                                i++;
                                colNum++;
                                continue;
                            }
                            else
                            {
                                cur.Append(c);
                                PushString();
                            }
                        }
                        else
                        {
                            cur.Append(c);
                        }
                    }
                    else
                    {
                        MakeError();
                    }
                    break;

                case '.':
                    if (status == LexType.Text)
                    {
                        cur.Append(c);
                    }
                    else if (status == LexType.Number)
                    {
                        if (DotWas)
                        {
                            throw new Exception("Two dot in number");
                        }
                        else
                        {
                            DotWas = true;
                            cur.Append(c);
                        }
                        break;
                    }
                    else if (status == LexType.Command)
                    {
                        cur.Append(c);
                        break;
                    }
                    else if (status == LexType.None)
                    {
                        if (res.Count > 0 && res.Last().LexemType == LexType.Text)
                        {
                            ReOpenString();
                            cur.Append(c);
                        }
                        else
                        {
                            MakeError();
                        }
                    }
                    else
                    {
                        MakeError();
                    }
                    break;

                default:
                    if (c >= '0' && c <= '9')
                    {
                        if (status == LexType.Number || status == LexType.Text || status == LexType.Command)
                        {
                            cur.Append(c);
                        }
                        else
                        {
                            PushString();
                            DotWas = false;
                            cur.Append(c);
                            status = LexType.Number;
                        }
                    }
                    else
                    {
                        //Какой-то символ, типа буквы
                        if (status == LexType.Command || status == LexType.Text)
                        {
                            cur.Append(c);
                        }
                        else
                        {
                            PushString();
                            status = LexType.Command;
                            cur.Append(c);
                        }
                    }
                    break;
                }
            }
            PushString();
        }
예제 #28
0
파일: FgdProvider.cs 프로젝트: silky/sledge
 public LexObject(int lineNum, int charNum, LexType type, string value = "")
 {
     LineNumber = lineNum;
     CharacterNumber = charNum;
     Type = type;
     Value = value;
 }