summaryrefslogtreecommitdiff
path: root/context/data/scite/context/lexers/scite-context-lexer-cpp.lua
blob: 7ad187eafb07cf70b4d56724a87effcde4343889 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
local info = {
    version   = 1.002,
    comment   = "scintilla lpeg lexer for cpp",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

-- looks liks the original cpp lexer but web ready (so nothing special here yet)

local P, R, S = lpeg.P, lpeg.R, lpeg.S

local lexer       = require("scite-context-lexer")
local context     = lexer.context
local patterns    = context.patterns

local token       = lexer.token
local exact_match = lexer.exact_match

local cpplexer    = lexer.new("cpp","scite-context-lexer-cpp")
local whitespace  = cpplexer.whitespace

local keywords = { -- copied from cpp.lua
    -- c
    "asm", "auto", "break", "case", "const", "continue", "default", "do", "else",
    "extern", "false", "for", "goto", "if", "inline", "register", "return",
    "sizeof", "static", "switch", "true", "typedef", "volatile", "while",
    "restrict",
    -- hm
    "_Bool", "_Complex", "_Pragma", "_Imaginary",
    -- c++.
    "catch", "class", "const_cast", "delete", "dynamic_cast", "explicit",
    "export", "friend", "mutable", "namespace", "new", "operator", "private",
    "protected", "public", "signals", "slots", "reinterpret_cast",
    "static_assert", "static_cast", "template", "this", "throw", "try", "typeid",
    "typename", "using", "virtual"
}

local datatypes = { -- copied from cpp.lua
    "bool", "char", "double", "enum", "float", "int", "long", "short", "signed",
    "struct", "union", "unsigned", "void"
}

local macros = { -- copied from cpp.lua
    "define", "elif", "else", "endif", "error", "if", "ifdef", "ifndef", "import",
    "include", "line", "pragma", "undef", "using", "warning"
}

local luatexs = {
    "word", "halfword", "quarterword", "scaled", "pointer", "glueratio",
}

local space         = patterns.space -- S(" \n\r\t\f\v")
local any           = patterns.any
local restofline    = patterns.restofline
local startofline   = patterns.startofline

local squote        = P("'")
local dquote        = P('"')
local period        = P(".")
local escaped       = P("\\") * P(1)
local slashes       = P("//")
local begincomment  = P("/*")
local endcomment    = P("*/")
local percent       = P("%")

local hexadecimal   = patterns.hexadecimal
local decimal       = patterns.decimal
local float         = patterns.float
local integer       = P("-")^-1 * (hexadecimal + decimal) -- also in patterns ?

local spacing       = token(whitespace, space^1)
local rest          = token("default", any)

local shortcomment  = token("comment", slashes * restofline^0)
local longcomment   = token("comment", begincomment * (1-endcomment)^0 * endcomment^-1)

local shortstring   = token("quote",  dquote) -- can be shared
                    * token("string", (escaped + (1-dquote))^0)
                    * token("quote",  dquote)
                    + token("quote",  squote)
                    * token("string", (escaped + (1-squote))^0)
                    * token("quote",  squote)

local number        = token("number", float + integer)

local validword     = R("AZ","az","__") * R("AZ","az","__","09")^0
local identifier    = token("default",validword)

local operator      = token("special", S("+-*/%^!=<>;:{}[]().&|?~"))

----- optionalspace = spacing^0

local p_keywords    = exact_match(keywords)
local p_datatypes   = exact_match(datatypes)
local p_macros      = exact_match(macros)
local p_luatexs     = exact_match(luatexs)

local keyword       = token("keyword", p_keywords)
local datatype      = token("keyword", p_datatypes)
local identifier    = token("default", validword)
local luatex        = token("command", p_luatexs)

local macro         = token("data", #P("#") * startofline * P("#") * S("\t ")^0 * p_macros)

cpplexer._rules = {
    { "whitespace",   spacing      },
    { "keyword",      keyword      },
    { "type",         datatype     },
    { "luatex",       luatex       },
    { "identifier",   identifier   },
    { "string",       shortstring  },
    { "longcomment",  longcomment  },
    { "shortcomment", shortcomment },
    { "number",       number       },
    { "macro",        macro        },
    { "operator",     operator     },
    { "rest",         rest         },
}

local web = lexer.loadluafile("scite-context-lexer-web-snippets")

if web then

    lexer.inform("supporting web snippets in cpp lexer")

    cpplexer._rules_web = {
        { "whitespace",   spacing      },
        { "keyword",      keyword      },
        { "type",         datatype     },
        { "luatex",       luatex       },
        { "identifier",   identifier   },
        { "string",       shortstring  },
        { "longcomment",  longcomment  },
        { "shortcomment", shortcomment },
        { "web",          web.pattern  },
        { "number",       number       },
        { "macro",        macro        },
        { "operator",     operator     },
        { "rest",         rest         },
    }

else

    lexer.report("not supporting web snippets in cpp lexer")

    cpplexer._rules_web = {
        { "whitespace",   spacing      },
        { "keyword",      keyword      },
        { "type",         datatype     },
        { "luatex",       luatex       },
        { "identifier",   identifier   },
        { "string",       shortstring  },
        { "longcomment",  longcomment  },
        { "shortcomment", shortcomment },
        { "number",       number       },
        { "macro",        macro        },
        { "operator",     operator     },
        { "rest",         rest         },
    }

end

cpplexer._tokenstyles = context.styleset

cpplexer._foldpattern = P("/*") + P("*/") + S("{}") -- separate entry else interference (singular?)

cpplexer._foldsymbols = {
    _patterns = {
        "[{}]",
        "/%*",
        "%*/",
    },
 -- ["data"] = { -- macro
 --     ["region"]    =  1,
 --     ["endregion"] = -1,
 --     ["if"]        =  1,
 --     ["ifdef"]     =  1,
 --     ["ifndef"]    =  1,
 --     ["endif"]     = -1,
 -- },
    ["special"] = { -- operator
        ["{"] =  1,
        ["}"] = -1,
    },
    ["comment"] = {
        ["/*"] =  1,
        ["*/"] = -1,
    }
}

-- -- by indentation:

cpplexer._foldpatterns = nil
cpplexer._foldsymbols  = nil

return cpplexer