summaryrefslogtreecommitdiff
path: root/tests/d.at
blob: 347e868c915eb40385239962efc6ca6d2ec186dc (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# D features tests.                      -*- Autotest -*-

# Copyright (C) 2020-2022 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

AT_BANNER([[D Features.]])

# AT_CHECK_D_MINIMAL([DIRECTIVES], [PARSER_ACTION], [POSITION_CLASS], [EXTRA_USER_CODE])
# ----------------------------------------------------------------------
# Check that a minimal parser with DIRECTIVES compiles in D.
# Put the D code in YYParser.d.
m4_define([AT_CHECK_D_MINIMAL],
[
AT_DATA([[YYParser.y]], [
%language "D"
%token END "end"
[$1]
%%
start: END {$2};
%%
[$4]
void main() {}
])
AT_BISON_CHECK([[-Wno-deprecated YYParser.y]])
AT_CHECK([[grep '[mb]4_' YYParser.y]], [1], [ignore])
AT_COMPILE_D([[YYParser]])
])

# AT_CHECK_D_MINIMAL_W_LEXER([1:DIRECTIVES],
#       [2:YYLEX_ACTION], [3:LEXER_BODY], [4:PARSER_ACTION], [5:VALUE_TYPE],
#       [6:POSITION_TYPE], [7:LOCATION_TYPE])
# ---------------------------------------------------------------------
# Check that a minimal parser with DIRECTIVES and a body for yylex()
# compiles in D.
m4_define([AT_CHECK_D_MINIMAL_W_LEXER],
[AT_CHECK_D_MINIMAL([$1], [], [], [

import std.range.primitives;
import std.stdio;

auto calcLexer(R)(R range)
  if (isInputRange!R && is (ElementType!R : dchar))
{
  return new CalcLexer!R(range);
}

auto calcLexer (File f)
{
  import std.algorithm : map, joiner;
  import std.utf : byDchar;

  return f.byChunk(1024)        // avoid making a syscall roundtrip per char
          .map!(chunk => cast(char[]) chunk) // because byChunk returns ubyte[]
          .joiner               // combine chunks into a single virtual range of char
          .calcLexer;           // forward to other overload
}

class CalcLexer(R) : Lexer
  if (isInputRange!R && is (ElementType!R : dchar))
{
  R input;

  this(R r) {
    input = r;
  }

  void yyerror(string s) {}

  Symbol yylex()
  {
    $2
  }
}
]
[
  $3
], [$4], [$6])])

# AT_CHECK_D_GREP([LINE], [COUNT=1])
# -------------------------------------
# Check that YYParser.d contains exactly COUNT lines matching ^LINE$
# with grep.
m4_define([AT_CHECK_D_GREP],
[AT_CHECK([grep -c '^$1$' YYParser.d], [ignore], [m4_default([$2], [1])
])])

## -------------------------------------- ##
## D parser class extends and implements. ##
## -------------------------------------- ##

AT_SETUP([D parser class extends and implements])
AT_KEYWORDS([d])

AT_CHECK_D_MINIMAL([])
AT_CHECK_D_GREP([[class YYParser]])

AT_CHECK_D_MINIMAL([%define api.parser.extends {BaseClass}], [], [], [class BaseClass {}])
AT_CHECK_D_GREP([[class YYParser : BaseClass]])

AT_CHECK_D_MINIMAL([%define api.parser.extends {Interface}], [], [], [interface Interface {}])
AT_CHECK_D_GREP([[class YYParser : Interface]])

AT_CHECK_D_MINIMAL([%code lexer
{
  Symbol yylex () {return Symbol();}
  void yyerror (string s) {import std.stdio;writeln(s);}
}], [], [], [])
AT_CHECK_D_GREP([[private class YYLexer: Lexer]])

AT_CHECK_D_MINIMAL(
[%define api.parser.extends {BaseClass}
%define api.parser.implements {Interface}], [], [],
[class BaseClass {}
interface Interface {}
])
AT_CHECK_D_GREP([[class YYParser : BaseClass, Interface]])

AT_CHECK_D_MINIMAL(
[%define api.parser.extends {BaseClass}
%define api.parser.implements {Interface1, Interface2}], [], [],
[class BaseClass {}
interface Interface1 {}
interface Interface2 {}
])
AT_CHECK_D_GREP([[class YYParser : BaseClass, Interface1, Interface2]])

AT_CLEANUP

## --------------------------------------------- ##
## D parser class api.token.raw true by default. ##
## --------------------------------------------- ##

AT_SETUP([D parser class api.token.raw true by default])
AT_KEYWORDS([d])

AT_CHECK_D_MINIMAL_W_LEXER([
%define api.token.raw true
%union { int ival; }], [return Symbol(TokenKind.END);])
AT_CHECK_D_GREP([[  END = 3,]])

AT_CHECK_D_MINIMAL_W_LEXER([
%define api.token.raw false
%union { int ival; }], [return Symbol(TokenKind.END);])
AT_CHECK_D_GREP([[  END = 258,]])

AT_CHECK_D_MINIMAL_W_LEXER([
%union { int ival; }], [return Symbol(TokenKind.END);])
AT_CHECK_D_GREP([[  END = 3,]])

AT_CLEANUP