Exemple #1
0
 public LexObject(int lineNum, int charNum, LexType type, string value = "")
 {
     LineNumber      = lineNum;
     CharacterNumber = charNum;
     Type            = type;
     Value           = value;
 }
Exemple #2
0
 public Output(FolderManager manager)
 {
     _manager    = manager;
     _lexFlags   = (LexType.Command | LexType.Function | LexType.Hook | LexType.HookCall);
     _outputFlag = OutputFlag.Replicate;
     _path       = Directory.GetCurrentDirectory() + "\\" + _manager.GetName();
 }
Exemple #3
0
 private Lexema LexChar(LexType type)
 {
     StartLexema();
     AddChar();
     SetLexType(type);
     return(EndLexema());
 }
Exemple #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;
 }
Exemple #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);
     }
 }
        // 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);
        }
Exemple #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;
 }
Exemple #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);
     }
 }
Exemple #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;
        }
Exemple #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);
        }
        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");
            }
        }
Exemple #12
0
 public void SetLexFlags(LexType flag)
 {
     _lexFlags = flag;
 }
Exemple #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());
        }
 public static Term Of(this LexType type) => new Term(type);
 public Term(LexType type)
 {
     Type = type;
 }
Exemple #16
0
 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();
 }
Exemple #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 + ".");
     }
 }
Exemple #18
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);
     }
 }
Exemple #19
0
 public Lexem(LexType type)
 {
     LexType = type;
     Value = 0;
     VarName = "";
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
Exemple #20
0
 public Lexem(float number)
 {
     LexType = LexType.Number;
     Value = number;
     VarName = "";
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
Exemple #21
0
 public Lex(int id, LexType lexType, string value)
 {
     Id      = id;
     LexType = lexType;
     Value   = value;
 }
Exemple #22
0
 private void SetLexType(LexType type)
 {
     curr.Type = type;
 }
Exemple #23
0
 public LexObject(LexType type, string value = "")
 {
     Type = type;
     Value = value;
 }
Exemple #24
0
 public Lexema(LexType lexType, int start, string value)
 {
     this.lexType = lexType;
     this.start = start;
     this.value = value;
 }
Exemple #25
0
 public Lexema(LexType lexType, int start)
     : this(lexType, start, null)
 {
 }
Exemple #26
0
 public Lexem(string name)
 {
     LexType = LexType.Variable;
     Value = 0;
     VarName = name;
     StartCoords = default(Nodes.Node.Coords);
     EndCoords = default(Nodes.Node.Coords);
     SetCoords();
 }
Exemple #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();
        }
Exemple #28
0
 public LexObject(int lineNum, int charNum, LexType type, string value = "")
 {
     LineNumber = lineNum;
     CharacterNumber = charNum;
     Type = type;
     Value = value;
 }