File: Op2tokenstream.h

package info (click to toggle)
kdevelop-pg-qt 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,144 kB
  • ctags: 3,624
  • sloc: cpp: 19,239; lex: 945; ansic: 716; yacc: 615; ruby: 68; sh: 14; lisp: 10; fortran: 6; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,594 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef OP2_TOKENSTREAM
#define OP2_TOKENSTREAM

#include "Op2parser.h"
#include <kdebug.h>
#include <QString>
#include <kdev-pg-token-stream.h>

namespace Op2
{

class TokenStream : public KDevPG::TokenStream
{
    QString m_content;
public:
    TokenStream(const QString& content) : m_content(content)
    {
        for(int i = 0; i != content.size(); ++i)
        {
            if(content[i] == ' ')
            {
                continue;
            }
            
            Parser::Token &t = next();
            t.begin = i;if(content[i] == '\n')
            {
                t.kind = Parser::Token_BR;
                t.end = i;
            }
            else if(content.mid(i, 3) == "bin")
            {
                t.kind = Parser::Token_BIN;
                t.end = i + 2;
            }
            else if(content.mid(i, 4) == "tern")
            {
                t.kind = Parser::Token_TERN;
                t.end = i + 3;
            }
            else if(content.mid(i, 3) == "pre")
            {
                t.kind = Parser::Token_PRE;
                t.end = i + 2;
            }
            else if(content.mid(i, 4) == "post")
            {
                t.kind = Parser::Token_POST;
                t.end = i + 3;
            }
            else if(content.mid(i, 5) == "paren")
            {
                t.kind = Parser::Token_PAREN;
                t.end = i + 4;
            }
            else if(content.mid(i, 4) == "left")
            {
                t.kind = Parser::Token_LEFT;
                t.end = i + 3;
            }
            else if(content.mid(i, 5) == "right")
            {
                t.kind = Parser::Token_RIGHT;
                t.end = i + 4;
            }
            else if(content[i] >= '0' && content[i] <= '9')
            {
                t.end = i + 1;
                while(t.end != content.size() && content[(uint)t.end] >= '0' && content[(uint)t.end] <= '9')
                    ++t.end;
                --t.end;
                t.kind = Parser::Token_NUMBER;
            }
            else
            {
                t.end = i;
                t.kind = Parser::Token_OPERATOR;
            }
            i = t.end;
            kDebug() << t.kind << " " << t.begin << " " << t.end;
        }
        Parser::Token &t = next();
        t.begin = content.size();
        t.end = content.size() - 1;
        t.kind = Parser::Token_EOF;
        rewind(0);
    }
    inline QString tokenString(uint pos)
    {
        return m_content.mid(token(pos).begin, token(pos).end - token(pos).begin + 1);
    }
};

}

#endif