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
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class D < RegexLexer
tag 'd'
aliases 'dlang'
filenames '*.d', '*.di'
mimetypes 'application/x-dsrc', 'text/x-dsrc'
title "D"
desc 'The D programming language(dlang.org)'
keywords = %w(
abstract alias align asm assert auto body
break case cast catch class const continue
debug default delegate delete deprecated do else
enum export extern finally final foreach_reverse
foreach for function goto if immutable import
interface invariant inout in is lazy mixin
module new nothrow out override package pragma
private protected public pure ref return scope
shared static struct super switch synchronized
template this throw try typedef typeid typeof
union unittest version volatile while with
__gshared __traits __vector __parameters
)
keywords_type = %w(
bool byte cdouble cent cfloat char creal
dchar double float idouble ifloat int ireal
long real short ubyte ucent uint ulong
ushort void wchar
)
keywords_pseudo = %w(
__FILE__ __FILE_FULL_PATH__ __MODULE__ __LINE__ __FUNCTION__
__PRETTY_FUNCTION__ __DATE__ __EOF__ __TIME__ __TIMESTAMP__
__VENDOR__ __VERSION__
)
state :whitespace do
rule %r/\n/m, Text
rule %r/\s+/m, Text
end
state :root do
mixin :whitespace
# Comments
rule %r(//.*), Comment::Single
rule %r(/(\\\n)?[*](.|\n)*?[*](\\\n)?/), Comment::Multiline
rule %r(/\+), Comment::Multiline, :nested_comment
# Keywords
rule %r/(#{keywords.join('|')})\b/, Keyword
rule %r/(#{keywords_type.join('|')})\b/, Keyword::Type
rule %r/(false|true|null)\b/, Keyword::Constant
rule %r/(#{keywords_pseudo.join('|')})\b/, Keyword::Pseudo
rule %r/macro\b/, Keyword::Reserved
rule %r/(string|wstring|dstring|size_t|ptrdiff_t)\b/, Name::Builtin
# Literals
# HexFloat
rule %r/0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)[pP][+\-]?[0-9_]+[fFL]?[i]?/, Num::Float
# DecimalFloat
rule %r/[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?/, Num::Float
rule %r/\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?/, Num::Float
# IntegerLiteral
# Binary
rule %r/0[Bb][01_]+/, Num::Bin
# Octal
# TODO: 0[0-7] isn't supported use octal![0-7] instead
rule %r/0[0-7_]+/, Num::Oct
# Hexadecimal
rule %r/0[xX][0-9a-fA-F_]+/, Num::Hex
# Decimal
rule %r/(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?/, Num::Integer
# CharacterLiteral
rule %r/'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'/, Str::Char
# StringLiteral
# WysiwygString
rule %r/r"[^"]*"[cwd]?/, Str
# Alternate WysiwygString
rule %r/`[^`]*`[cwd]?/, Str
# DoubleQuotedString
rule %r/"(\\\\|\\"|[^"])*"[cwd]?/, Str
# EscapeSequence
rule %r/\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)/, Str
# HexString
rule %r/x"[0-9a-fA-F_\s]*"[cwd]?/, Str
# DelimitedString
rule %r/q"\[/, Str, :delimited_bracket
rule %r/q"\(/, Str, :delimited_parenthesis
rule %r/q"</, Str, :delimited_angle
rule %r/q"\{/, Str, :delimited_curly
rule %r/q"([a-zA-Z_]\w*)\n.*?\n\1"/, Str
rule %r/q"(.).*?\1"/, Str
# TokenString
rule %r/q\{/, Str, :token_string
# Attributes
rule %r/@([a-zA-Z_]\w*)?/, Name::Decorator
# Tokens
rule %r`(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>=|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]`, Punctuation
# Identifier
rule %r/[a-zA-Z_]\w*/, Name
# Line
rule %r/#line\s.*\n/, Comment::Special
end
state :nested_comment do
rule %r([^+/]+), Comment::Multiline
rule %r(/\+), Comment::Multiline, :push
rule %r(\+/), Comment::Multiline, :pop!
rule %r([+/]), Comment::Multiline
end
state :token_string do
rule %r/\{/, Punctuation, :token_string_nest
rule %r/\}/, Str, :pop!
mixin :root
end
state :token_string_nest do
rule %r/\{/, Punctuation, :push
rule %r/\}/, Punctuation, :pop!
mixin :root
end
state :delimited_bracket do
rule %r/[^\[\]]+/, Str
rule %r/\[/, Str, :delimited_inside_bracket
rule %r/\]"/, Str, :pop!
end
state :delimited_inside_bracket do
rule %r/[^\[\]]+/, Str
rule %r/\[/, Str, :push
rule %r/\]/, Str, :pop!
end
state :delimited_parenthesis do
rule %r/[^()]+/, Str
rule %r/\(/, Str, :delimited_inside_parenthesis
rule %r/\)"/, Str, :pop!
end
state :delimited_inside_parenthesis do
rule %r/[^()]+/, Str
rule %r/\(/, Str, :push
rule %r/\)/, Str, :pop!
end
state :delimited_angle do
rule %r/[^<>]+/, Str
rule %r/</, Str, :delimited_inside_angle
rule %r/>"/, Str, :pop!
end
state :delimited_inside_angle do
rule %r/[^<>]+/, Str
rule %r/</, Str, :push
rule %r/>/, Str, :pop!
end
state :delimited_curly do
rule %r/[^{}]+/, Str
rule %r/\{/, Str, :delimited_inside_curly
rule %r/\}"/, Str, :pop!
end
state :delimited_inside_curly do
rule %r/[^{}]+/, Str
rule %r/\{/, Str, :push
rule %r/\}/, Str, :pop!
end
end
end
end
|