forked from JuliaDocs/Highlights.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexers.jl
More file actions
125 lines (95 loc) · 2.65 KB
/
Copy pathLexers.jl
File metadata and controls
125 lines (95 loc) · 2.65 KB
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
"""
An exported submodule that provides a selection of lexer definitions for tokenising source
code. The following names are exported from the module:
$(EXPORTS)
"""
module Lexers
using DocStringExtensions
import ..Highlights: AbstractLexer
import ..Highlights.Compiler
# Public Interface.
export AbstractLexer, @lexer
"""
$(SIGNATURES)
Declare the lexer definition for a custom lexer `T` using a `Dict` object `dict`. `dict`
must either be a `Dict` literal or an expression that returns a `Dict` -- such as a `let`
block.
# Examples
```jldoctest
julia> using Highlights.Lexers
julia> abstract type CustomLexer <: AbstractLexer end
julia> @lexer CustomLexer Dict(
:name => "Custom",
:tokens => Dict(
# ...
)
);
```
"""
macro lexer(T, dict)
tx, dx = map(esc, (T, dict))
quote
let data = $(Compiler).LexerData($dx)
$(Compiler).metadata(::Type{$tx}) = data
end
# let data = getdata($tx)
# @generated $(Compiler).lex!{S}(ctx::Context, ::Type{$tx}, ::State{S}) =
# compile($tx, S, data)
# end
$(Compiler).compile_lexer($(__module__), $tx)
$tx
end
end
# Utilities.
"""
$(SIGNATURES)
Build a regular expression from a vector of strings. Useful for keyword lists.
The keyword arguments `prefix` and `suffix` can be used to add patterns before and after the
main match group.
# Examples
```jldoctest
julia> import Highlights.Lexers: words
julia> words(["if", "else"])
r"(if|else)"
```
"""
words(vec; prefix="", suffix="") = Regex(string(prefix, "(", join(vec, '|'), ")", suffix))
"""
$(SIGNATURES)
A utility string macro to avoid having to escape strings used to build a regular expression.
"""
macro raw_str(str)
str
end
# Names.
export
FortranLexer,
JuliaLexer,
JuliaConsoleLexer,
MatlabLexer,
PTXLexer,
RLexer,
TOMLLexer,
"A FORTRAN 90 source code lexer."
abstract type FortranLexer <: AbstractLexer end
"A lexer for Julia source code."
abstract type JuliaLexer <: AbstractLexer end
"A lexer for Julia REPL sessions."
abstract type JuliaConsoleLexer <: AbstractLexer end
"A lexer for MATLAB source code."
abstract type MatlabLexer <: AbstractLexer end
"PTX ISA(NVIDIA GPU) Lexer"
abstract type PTXLexer <: AbstractLexer end
"A lexer for the R language."
abstract type RLexer <: AbstractLexer end
"TOML (Tom's Obvious, Minimal Language) lexer."
abstract type TOMLLexer <: AbstractLexer end
# Definitions.
using ..Highlights.Tokens
include("lexers/fortran.jl")
include("lexers/julia.jl")
include("lexers/matlab.jl")
include("lexers/ptx.jl")
include("lexers/r.jl")
include("lexers/toml.jl")
end # module