Source

semantic / semantic-grammar.wy

Full commit
  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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
;;
;; Copyright (C) 2002, 2003, 2004 David Ponce
;;
;; Author: David Ponce <david@dponce.com>
;; Maintainer: David Ponce <david@dponce.com>
;; Created: 26 Aug 2002
;; Keywords: syntax
;; X-RCS: $Id$
;;
;; This file is not part of GNU Emacs.
;;
;; 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 2, or (at
;; your option) any later version.
;;
;; This software 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

%{
  ;; Current parsed nonterminal name.
  (defvar semantic-grammar-wy--nterm nil)
  ;; Index of rule in a nonterminal clause.
  (defvar semantic-grammar-wy--rindx nil)
  }

;;%package semantic-grammar-wy

%languagemode wy-mode

;; Main
%start grammar
;; Reparse
%start prologue epilogue declaration nonterminal rule
;; EXPANDFULL
%start put_names put_values use_names

;; Keywords
%type    <keyword>
%keyword DEFAULT-PREC    "%default-prec"
%keyword NO-DEFAULT-PREC "%no-default-prec"
%keyword KEYWORD         "%keyword"
%keyword LANGUAGEMODE    "%languagemode"
%keyword LEFT            "%left"
%keyword NONASSOC        "%nonassoc"
%keyword PACKAGE         "%package"
%keyword PREC            "%prec"
%keyword PUT             "%put"
%keyword QUOTEMODE       "%quotemode"
%keyword RIGHT           "%right"
%keyword SCOPESTART      "%scopestart"
%keyword START           "%start"
%keyword TOKEN           "%token"
%keyword TYPE            "%type"
%keyword USE-MACROS      "%use-macros"

;; Literals
%type  <string>
%token <string>      STRING

%type  <symbol>      syntax ":?\\(\\sw\\|\\s_\\)+"
%token <symbol>      SYMBOL
%token <symbol>      PERCENT_PERCENT "\\`%%\\'"

%type  <char>        syntax semantic-grammar-lex-c-char-re
%token <char>        CHARACTER

%type  <qlist>       matchdatatype sexp syntax "\\s'\\s-*("
%token <qlist>       PREFIXED_LIST

%type  <sexp>        matchdatatype sexp syntax "\\="
%token <sexp>        SEXP

;; Don't generate these analyzers which needs special handling code.
%token <code>        PROLOGUE "%{...%}"
%token <code>        EPILOGUE "%%...EOF"

;; Blocks & Parenthesis
%type  <block>
%token <block>       PAREN_BLOCK "(LPAREN RPAREN)"
%token <block>       BRACE_BLOCK "(LBRACE RBRACE)"
%token <open-paren>  LPAREN      "("
%token <close-paren> RPAREN      ")"
%token <open-paren>  LBRACE      "{"
%token <close-paren> RBRACE      "}"

;; Punctuations
%type  <punctuation>
%token <punctuation> COLON       ":"
%token <punctuation> SEMI        ";"
%token <punctuation> OR          "|"
%token <punctuation> LT          "<"
%token <punctuation> GT          ">"

%%

grammar:
    prologue
  | epilogue
  | declaration
  | nonterminal
  | PERCENT_PERCENT
  ;

;;; Prologue/Epilogue
;;
prologue:
    PROLOGUE
    (CODE-TAG "prologue" nil)
  ;

epilogue:
    EPILOGUE
    (CODE-TAG "epilogue" nil)
  ;

;;; Declarations
;;
declaration:
    decl
    (eval $1)
  ;

decl:
    default_prec_decl
  | no_default_prec_decl
  | languagemode_decl
  | package_decl
  | precedence_decl
  | put_decl
  | quotemode_decl
  | scopestart_decl
  | start_decl
  | keyword_decl
  | token_decl
  | type_decl
  | use_macros_decl
  ;

default_prec_decl:
    DEFAULT-PREC
    `(TAG "default-prec" 'assoc :value '("t"))
  ;

no_default_prec_decl:
    NO-DEFAULT-PREC
    `(TAG "default-prec" 'assoc :value '("nil"))
  ;

languagemode_decl:
    LANGUAGEMODE symbols
    `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
  ;

package_decl:
    PACKAGE SYMBOL
    `(PACKAGE-TAG ',$2 nil)
  ;

precedence_decl:
    associativity token_type_opt items
    `(TAG ',$1 'assoc :type ',$2 :value ',$3)
  ;

associativity:
    LEFT
    (progn "left")
  | RIGHT
    (progn "right")
  | NONASSOC
    (progn "nonassoc")
  ;

put_decl:
    PUT put_name put_value
    `(TAG ',$2 'put :value ',(list $3))
  | PUT put_name put_value_list
    `(TAG ',$2 'put :value ',$3)
  | PUT put_name_list put_value
    `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
  | PUT put_name_list put_value_list
    `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
  ;

put_name_list:
    BRACE_BLOCK
    (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
  ;

put_names:
    LBRACE
    ()
  | RBRACE
    ()
  | put_name
 ;; Must return a list of Semantic tags to EXPANDFULL!
    (TAG $1 'put-name)
  ;

put_name:
    SYMBOL
  | token_type
  ;

put_value_list:
    BRACE_BLOCK
    (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
  ;

put_values:
    LBRACE
    ()
  | RBRACE
    ()
  | put_value
 ;; Must return a list of Semantic tags to EXPANDFULL!
    (CODE-TAG "put-value" $1)
  ;

put_value:
    SYMBOL any_value
    (cons $1 $2)
  ;

scopestart_decl:
    SCOPESTART SYMBOL
    `(TAG ',$2 'scopestart)
  ;

quotemode_decl:
    QUOTEMODE SYMBOL
    `(TAG ',$2 'quotemode)
  ;

start_decl:
    START symbols
    `(TAG ',(car $2) 'start :rest ',(cdr $2))
  ;

keyword_decl:
    KEYWORD SYMBOL string_value
    `(TAG ',$2 'keyword :value ',$3)
  ;

token_decl:
    TOKEN token_type_opt SYMBOL string_value
    `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
  | TOKEN token_type_opt symbols
    `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
  ;

token_type_opt:
 ;; EMPTY
  | token_type
  ;

token_type:
    LT SYMBOL GT
    (progn $2)
  ;

type_decl:
    TYPE token_type plist_opt
    `(TAG ',$2 'type :value ',$3)
  ;

plist_opt:
 ;;EMPTY
  | plist
  ;

plist:
    plist put_value
    (append (list $2) $1)
  | put_value
    (list $1)
  ;

use_name_list:
    BRACE_BLOCK
    (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
  ;

use_names:
    LBRACE
    ()
  | RBRACE
    ()
  | SYMBOL
 ;; Must return a list of Semantic tags to EXPANDFULL!
    (TAG $1 'use-name)
  ;

use_macros_decl:
    USE-MACROS SYMBOL use_name_list
    `(TAG "macro" 'macro :type ',$2 :value ',$3)
  ;

string_value:
    STRING
    (read $1)
  ;

;; Return a Lisp readable form
any_value:
    SYMBOL
  | STRING
  | PAREN_BLOCK
  | PREFIXED_LIST
  | SEXP
  ;

symbols:
    lifo_symbols
    (nreverse $1)
  ;

lifo_symbols:
    lifo_symbols SYMBOL
    (cons $2 $1)
  | SYMBOL
    (list $1)
  ;

;;; Grammar rules
;;
nonterminal:
    SYMBOL
    (setq semantic-grammar-wy--nterm $1
          semantic-grammar-wy--rindx 0)
    COLON rules SEMI
    (TAG $1 'nonterminal :children $4)
  ;

rules:
    lifo_rules
    (apply 'nconc (nreverse $1))
  ;

lifo_rules:
    lifo_rules OR rule
    (cons $3 $1)
  | rule
    (list $1)
  ;

rule:
    rhs
    (let* ((nterm semantic-grammar-wy--nterm)
           (rindx semantic-grammar-wy--rindx)
           (rhs   $1)
           comps prec action elt)
      (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
      (while rhs
        (setq elt (car rhs)
              rhs (cdr rhs))
        (cond
         ;; precedence level
         ((vectorp elt)
          (if prec
              (error "duplicate %%prec in `%s:%d' rule" nterm rindx))
          (setq prec (aref elt 0)))
         ;; action
         ((consp elt)
          ;; don't forget that rhs items are in reverse order, so
          ;; the end-of-rule semantic action is the first item.
          (if (or action comps)
              ;; a mid-rule action
              (setq comps (cons elt comps)
                    ;; keep rule and action index synchronized
                    semantic-grammar-wy--rindx
                    (1+ semantic-grammar-wy--rindx))
            ;; the end-of-rule action
            (setq action (car elt))))
         ;; item
         (t
          (setq comps (cons elt comps)))))
      (EXPANDTAG
       (TAG (format "%s:%d" nterm rindx) 'rule
            :type (if comps "group" "empty")
            :value comps :prec prec :expr action)))
  ;

rhs:
 ;; EMPTY
  | rhs item
    (cons $2 $1)
  | rhs action
    (cons (list $2) $1)
  | rhs PREC item
    (cons (vector $3) $1)
  ;

action:
    PAREN_BLOCK
  | PREFIXED_LIST
  | BRACE_BLOCK
    (format "(progn\n%s)"
            (let ((s $1))
              (if (string-match "^{[\r\n\t ]*" s)
                  (setq s (substring s (match-end 0))))
              (if (string-match "[\r\n\t ]*}$" s)
                  (setq s (substring s 0 (match-beginning 0))))
              s))
  ;

items:
    lifo_items
    (nreverse $1)
  ;

lifo_items:
    lifo_items item
    (cons $2 $1)
  | item
    (list $1)
  ;

item:
    SYMBOL
  | CHARACTER
  ;

%%

(define-lex semantic-grammar-lexer
  "Lexical analyzer that handles Semantic grammar buffers.
It ignores whitespaces, newlines and comments."
  semantic-lex-ignore-newline
  semantic-lex-ignore-whitespace
  ;; Must detect prologue/epilogue before other symbols/keywords!
  semantic-grammar-lex-prologue
  semantic-grammar-lex-epilogue
  semantic-grammar-wy--<keyword>-keyword-analyzer
  semantic-grammar-wy--<symbol>-regexp-analyzer
  semantic-grammar-wy--<char>-regexp-analyzer
  semantic-grammar-wy--<string>-sexp-analyzer
  ;; Must detect comments after strings because `comment-start-skip'
  ;; regexp match semicolons inside strings!
  semantic-lex-ignore-comments
  ;; Must detect prefixed list before punctuation because prefix chars
  ;; are also punctuations!
  semantic-grammar-wy--<qlist>-sexp-analyzer
  ;; Must detect punctuations after comments because the semicolon can
  ;; be a punctuation or a comment start!
  semantic-grammar-wy--<punctuation>-string-analyzer
  semantic-grammar-wy--<block>-block-analyzer
  semantic-grammar-wy--<sexp>-sexp-analyzer
  )

;;; semantic-grammar.wy ends here