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
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class YANG < RegexLexer
title 'YANG'
desc "Lexer for the YANG 1.1 modeling language (RFC7950)"
tag 'yang'
filenames '*.yang'
mimetypes 'application/yang'
id = /[\w-]+(?=[^\w\-\:])\b/
#Keywords from RFC7950 ; oriented at BNF style
def self.top_stmts_keywords
@top_stms_keywords ||= Set.new %w(
module submodule
)
end
def self.module_header_stmts_keywords
@module_header_stmts_keywords ||= Set.new %w(
belongs-to namespace prefix yang-version
)
end
def self.meta_stmts_keywords
@meta_stmts_keywords ||= Set.new %w(
contact description organization reference revision
)
end
def self.linkage_stmts_keywords
@linkage_stmts_keywords ||= Set.new %w(
import include revision-date
)
end
def self.body_stmts_keywords
@body_stms_keywords ||= Set.new %w(
action argument augment deviation extension feature grouping identity
if-feature input notification output rpc typedef
)
end
def self.data_def_stmts_keywords
@data_def_stms_keywords ||= Set.new %w(
anydata anyxml case choice config container deviate leaf leaf-list
list must presence refine uses when
)
end
def self.type_stmts_keywords
@type_stmts_keywords ||= Set.new %w(
base bit default enum error-app-tag error-message fraction-digits
length max-elements min-elements modifier ordered-by path pattern
position range require-instance status type units value yin-element
)
end
def self.list_stmts_keywords
@list_stmts_keywords ||= Set.new %w(
key mandatory unique
)
end
#RFC7950 other keywords
def self.constants_keywords
@constants_keywords ||= Set.new %w(
add current delete deprecated false invert-match max min
not-supported obsolete replace true unbounded user
)
end
#RFC7950 Built-In Types
def self.types
@types ||= Set.new %w(
binary bits boolean decimal64 empty enumeration identityref
instance-identifier int16 int32 int64 int8 leafref string uint16
uint32 uint64 uint8 union
)
end
state :comment do
rule %r/[^*\/]/, Comment
rule %r/\/\*/, Comment, :comment
rule %r/\*\//, Comment, :pop!
rule %r/[*\/]/, Comment
end
#Keyword::Reserved
#groups Name::Tag, Text::Whitespace
state :root do
rule %r/\s+/, Text::Whitespace
rule %r/[\{\}\;]+/, Punctuation
rule %r/(?<![\-\w])(and|or|not|\+|\.)(?![\-\w])/, Operator
rule %r/"(?:\\"|[^"])*?"/, Str::Double #for double quotes
rule %r/'(?:\\'|[^'])*?'/, Str::Single #for single quotes
rule %r/\/\*/, Comment, :comment
rule %r/\/\/.*?$/, Comment
#match BNF stmt for `node-identifier` with [ prefix ":"]
rule %r/(?:^|(?<=[\s{};]))([\w.-]+)(:)([\w.-]+)(?=[\s{};])/ do
groups Name::Namespace, Punctuation, Name
end
#match BNF stmt `date-arg-str`
rule %r/([0-9]{4}\-[0-9]{2}\-[0-9]{2})(?=[\s\{\}\;])/, Name::Label
rule %r/([0-9]+\.[0-9]+)(?=[\s\{\}\;])/, Num::Float
rule %r/([0-9]+)(?=[\s\{\}\;])/, Num::Integer
rule id do |m|
name = m[0].downcase
if self.class.top_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.module_header_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.meta_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.linkage_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.body_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.data_def_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.type_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.list_stmts_keywords.include? name
token Keyword::Declaration
elsif self.class.types.include? name
token Keyword::Type
elsif self.class.constants_keywords.include? name
token Name::Constant
else
token Name
end
end
rule %r/[^;{}\s'"]+/, Name
end
end
end
end
|