File: ada_spec.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (164 lines) | stat: -rw-r--r-- 6,609 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Ada do
  let(:subject) { Rouge::Lexers::Ada.new }

  describe 'guessing' do
    include Support::Guessing

    it 'guesses by filename' do
      assert_guess :filename => 'foo.ada'
      assert_guess :filename => 'foo.adb'
      assert_guess :filename => 'foo.ads'
    end

    it 'guesses by mimetype' do
      assert_guess :mimetype => 'text/x-ada'
    end
  end

  describe 'lexing' do
    include Support::Lexing

    it 'classifies identifiers' do
      assert_tokens_equal 'constant Boolean := A and B',
                           ['Keyword', 'constant'],
                           ['Text', ' '],
                           ['Name.Builtin', 'Boolean'],
                           ['Text', ' '],
                           ['Operator', ':='],
                           ['Text', ' '],
                           ['Name', 'A'],
                           ['Text', ' '],
                           ['Operator.Word', 'and'],
                           ['Text', ' '],
                           ['Name', 'B']
    end

    it 'accepts Unicode identifiers' do
      assert_tokens_equal '東京', ['Name', '東京']
      assert_tokens_equal '0東京', ['Error', '0'], ['Name', '東京']
      assert_tokens_equal '東京0', ['Name', '東京0']
    end

    it 'rejects identifiers with double or trailing underscores' do
      assert_tokens_equal '_ab', ['Error', '_ab']
      assert_tokens_equal 'a__b', ['Error', 'a__b']
      assert_tokens_equal 'a_b', ['Name', 'a_b']
      assert_tokens_equal 'ab_', ['Error', 'ab_']
    end

    it 'understands other connecting punctuation' do
      assert_tokens_equal 'a﹏b', ['Name', 'a﹏b']
      assert_tokens_equal '﹏ab', ['Error', '﹏ab']
      assert_tokens_equal 'a﹏﹏b', ['Error', 'a﹏﹏b']
      assert_tokens_equal 'ab﹏', ['Error', 'ab﹏']
    end

    it 'classifies based number literals' do
      assert_tokens_equal '2#0001_1110#', ['Literal.Number.Bin', '2#0001_1110#']
      assert_tokens_equal '2#0001__1110#', ['Error', '2#0001__1110#']
      assert_tokens_equal '8#1234_0000#', ['Literal.Number.Oct', '8#1234_0000#']
      assert_tokens_equal '16#abc_BBB_12#', ['Literal.Number.Hex', '16#abc_BBB_12#']
      assert_tokens_equal '4#1230000#e+5', ['Literal.Number.Integer', '4#1230000#e+5']
      assert_tokens_equal '2#0001_1110#e3', ['Literal.Number.Bin', '2#0001_1110#e3']

      assert_tokens_equal '16#abc_BBB.12#', ['Literal.Number.Float', '16#abc_BBB.12#']
    end

    it 'recognizes exponents in integers and reals' do
      assert_tokens_equal '1e6', ['Literal.Number.Integer', '1e6']
      assert_tokens_equal '123_456', ['Literal.Number.Integer', '123_456']
      assert_tokens_equal '3.14159_26', ['Literal.Number.Float', '3.14159_26']
      assert_tokens_equal '3.141_592e-20', ['Literal.Number.Float', '3.141_592e-20']
    end

    it 'highlights escape sequences inside doubly quoted strings' do
      assert_tokens_equal '"Archimedes said ""Εύρηκα"""',
                          ['Literal.String.Double', '"Archimedes said '],
                          ['Literal.String.Escape', '""'],
                          ['Literal.String.Double', 'Εύρηκα'],
                          ['Literal.String.Escape', '""'],
                          ['Literal.String.Double', '"']
    end

    it 'marks function names in declarations' do
      assert_tokens_equal 'Entry Foo IS',
                          ['Keyword.Declaration', 'Entry'],
                          ['Text', ' '],
                          ['Name.Function', 'Foo'],
                          ['Text', ' '],
                          ['Keyword', 'IS']

      assert_tokens_equal 'package body Ada.Foo IS',
                          ['Keyword.Declaration', 'package'],
                          ['Text', ' '],
                          ['Keyword.Declaration', 'body'],
                          ['Text', ' '],
                          ['Name.Namespace', 'Ada'],
                          ['Punctuation', '.'],
                          ['Name.Function', 'Foo'],
                          ['Text', ' '],
                          ['Keyword', 'IS']
    end

    it 'allows both names and builtin names in context clauses' do
      assert_tokens_equal 'limited with Math.Integer;',
                          ['Keyword.Namespace', 'limited'],
                          ['Text', ' '],
                          ['Keyword.Namespace', 'with'],
                          ['Text', ' '],
                          ['Name.Namespace', 'Math'],
                          ['Punctuation', '.'],
                          ['Name.Namespace', 'Integer'],
                          ['Punctuation', ';']
    end

    it 'recovers quickly after mistakenly entering :libunit_name' do
      # A `with` keyword at the beginning of a line is 99.9% sure to be
      # a context clause, but there are things that could be mistaken if
      # they are indented strangely:
      #
      # generic
      #    with function Random return Integer is <>;
      # procedure Foo;
      #
      # If that `with` is not indented, we should recover immediately:
      assert_tokens_equal 'with function Random',
                          ['Keyword.Namespace', 'with'],
                          ['Text', ' '],
                          ['Keyword.Declaration', 'function'],
                          ['Text', ' '],
                          ['Name.Function', 'Random']

      # Another case that's even less likely to be at BOL:
      #
      # type Painted_Point is new Point with
      #    record
      #       Paint : Color := White;
      #    end record;
      #
      # type Addition is new Binary_Operation with null record;
      assert_tokens_equal 'with record Paint',
                          ['Keyword.Namespace', 'with'],
                          ['Text', ' '],
                          ['Keyword', 'record'],
                          ['Text', ' '],
                          ['Name', 'Paint']
      assert_tokens_equal 'with null record;',
                          ['Keyword.Namespace', 'with'],
                          ['Text', ' '],
                          ['Keyword', 'null'],
                          ['Text', ' '],
                          ['Keyword', 'record'],
                          ['Punctuation', ';']

      # Finally: raise Runtime_Error with "NO!"
      assert_tokens_equal 'with "NO!"',
                          ['Keyword.Namespace', 'with'],
                          ['Text', ' '],
                          ['Literal.String.Double', '"NO!"']
    end
  end
end