summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/xquery.rb
blob: eb0d7a766217d44beb76be9333c4c4ece50d7c20 (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
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    load_lexer 'xpath.rb'
    class XQuery < XPath
      title 'XQuery'
      desc 'XQuery 3.1: An XML Query Language'
      tag 'xquery'
      filenames '*.xquery', '*.xq', '*.xqm'
      mimetypes 'application/xquery'

      def self.keywords
        @keywords ||= Regexp.union super, Regexp.union(%w(
          xquery encoding version declare module
          namespace copy-namespaces boundary-space construction
          default collation base-uri preserve strip
          ordering ordered unordered order empty greatest least
          preserve no-preserve inherit no-inherit
          decimal-format decimal-separator grouping-separator
          infinity minus-sign NaN percent per-mille
          zero-digit digit pattern-separator exponent-separator
          import schema at element option
          function external context item
          typeswitch switch case
          try catch
          validate lax strict type
          document element attribute text comment processing-instruction
          for let where order group by return
          allowing tumbling stable sliding window
          start end only when previous next count collation
          ascending descending
        ))
      end

      # Mixin states:

      state :tags do
        rule %r/<#{XPath.qName}/, Name::Tag, :start_tag
        rule %r/<!--/, Comment, :xml_comment
        rule %r/<\?.*?\?>/, Comment::Preproc
        rule %r/<!\[CDATA\[.*?\]\]>/, Comment::Preproc
        rule %r/&\S*?;/, Name::Entity
      end

      # Lexical states:

      prepend :root do
        mixin :tags

        rule %r/\{/, Punctuation
        rule %r/\}`?/ do
          token Punctuation
          if stack.length > 1
            pop!
          end
        end

        rule %r/(namespace)(\s+)(#{XPath.ncName})/ do
          groups Keyword, Text::Whitespace, Name::Namespace
        end

        rule %r/(#{XQuery.keywords})\b/, Keyword
        rule %r/;/, Punctuation
        rule %r/%/, Keyword::Declaration, :annotation

        rule %r/(\(#)(\s*)(#{XPath.eqName})/ do
          groups Comment::Preproc, Text::Whitespace, Name::Tag
          push :pragma
        end

        rule %r/``\[/, Str, :str_constructor
      end

      state :annotation do
        mixin :commentsAndWhitespace
        rule XPath.eqName, Keyword::Declaration, :pop!
      end

      state :pragma do
        mixin :commentsAndWhitespace
        rule %r/#\)/, Comment::Preproc, :pop!
        rule %r/./, Comment::Preproc
      end

      # https://www.w3.org/TR/xquery-31/#id-string-constructors
      state :str_constructor do
        rule %r/`\{/, Punctuation, :root
        rule %r/\]``/, Str, :pop!
        rule %r/[^`\]]+/m, Str
        rule %r/[`\]]/, Str
      end

      state :xml_comment do
        rule %r/[^-]+/m, Comment
        rule %r/-->/, Comment, :pop!
        rule %r/-/, Comment
      end

      state :start_tag do
        rule %r/\s+/m, Text::Whitespace
        rule %r/([\w.:-]+\s*=)(")/m do
          groups Name::Attribute, Str
          push :quot_attr
        end
        rule %r/([\w.:-]+\s*=)(')/m do
          groups Name::Attribute, Str
          push :apos_attr
        end
        rule %r/>/, Name::Tag, :tag_content
        rule %r(/>), Name::Tag, :pop!
      end

      state :quot_attr do
        rule %r/"/, Str, :pop!
        rule %r/\{\{/, Str
        rule %r/\{/, Punctuation, :root
        rule %r/[^"{>]+/m, Str
      end

      state :apos_attr do
        rule %r/'/, Str, :pop!
        rule %r/\{\{/, Str
        rule %r/\{/, Punctuation, :root
        rule %r/[^'{>]+/m, Str
      end

      state :tag_content do
        rule %r/\s+/m, Text::Whitespace
        mixin :tags

        rule %r/(\{\{|\}\})/, Text
        rule %r/\{/, Punctuation, :root

        rule %r/[^{}<&]/, Text

        rule %r(</#{XPath.qName}(\s*)>) do
          token Name::Tag
          pop! 2 # pop self and tag_start
        end
      end
    end
  end
end