-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemantic-lex.el
2098 lines (1919 loc) · 81.3 KB
/
semantic-lex.el
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; semantic-lex.el --- Lexical Analyzer builder
;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Eric M. Ludlam
;; X-CVS: $Id: semantic-lex.el,v 1.54 2009/02/26 03:11:12 zappo Exp $
;; This file is not part of GNU Emacs.
;; Semantic 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.
;;; Commentary:
;;
;; This file handles the creation of lexical analyzers for different
;; languages in Emacs Lisp. The purpose of a lexical analyzer is to
;; convert a buffer into a list of lexical tokens. Each token
;; contains the token class (such as 'number, 'symbol, 'IF, etc) and
;; the location in the buffer it was found. Optionally, a token also
;; contains a string representing what is at the designated buffer
;; location.
;;
;; Tokens are pushed onto a token stream, which is basically a list of
;; all the lexical tokens from the analyzed region. The token stream
;; is then handed to the grammar which parsers the file.
;;
;;; How it works
;;
;; Each analyzer specifies a condition and forms. These conditions
;; and forms are assembled into a function by `define-lex' that does
;; the lexical analysis.
;;
;; In the lexical analyzer created with `define-lex', each condition
;; is tested for a given point. When the conditin is true, the forms
;; run.
;;
;; The forms can push a lexical token onto the token stream. The
;; analyzer forms also must move the current analyzer point. If the
;; analyzer point is moved without pushing a token, then tne matched
;; syntax is effectively ignored, or skipped.
;;
;; Thus, starting at the beginning of a region to be analyzed, each
;; condition is tested. One will match, and a lexical token might be
;; pushed, and the point is moved to the end of the lexical token
;; identified. At the new position, the process occurs again until
;; the end of the specified region is reached.
;;
;;; How to use semantic-lex
;;
;; To create a lexer for a language, use the `define-lex' macro.
;;
;; The `define-lex' macro accepts a list of lexical analyzers. Each
;; analyzer is created with `define-lex-analyzer', or one of the
;; derivitive macros. A single analyzer defines a regular expression
;; to match text in a buffer, and a short segment of code to create
;; one lexical token.
;;
;; Each analyzer has a NAME, DOC, a CONDITION, and possibly some
;; FORMS. The NAME is the name used in `define-lex'. The DOC
;; describes what the analyzer should do.
;;
;; The CONDITION evaluates the text at the current point in the
;; current buffer. If CONDITION is true, then the FORMS will be
;; executed.
;;
;; The purpose of the FORMS is to push new lexical tokens onto the
;; list of tokens for the current buffer, and to move point after the
;; matched text.
;;
;; Some macros for creating one analyzer are:
;;
;; define-lex-analyzer - A generic analyzer associating any style of
;; condition to forms.
;; define-lex-regex-analyzer - Matches a regular expression.
;; define-lex-simple-regex-analyzer - Matches a regular expressions,
;; and pushes the match.
;; define-lex-block-analyzer - Matches list syntax, and defines
;; handles open/close delimiters.
;;
;; These macros are used by the grammar compiler when lexical
;; information is specified in a grammar:
;; define-lex- * -type-analyzer - Matches syntax specified in
;; a grammar, and pushes one token for it. The * would
;; be `sexp' for things like lists or strings, and
;; `string' for things that need to match some special
;; string, such as "\\." where a literal match is needed.
;;
;;; Lexical Tables
;;
;; There are tables of different symbols managed in semantic-lex.el.
;; They are:
;;
;; Lexical keyword table - A Table of symbols declared in a grammar
;; file with the %keyword declaration.
;; Keywords are used by `semantic-lex-symbol-or-keyword'
;; to create lexical tokens based on the keyword.
;;
;; Lexical type table - A table of symbols declared in a grammer
;; file with the %type declaration.
;; The grammar compiler uses the type table to create new
;; lexical analyzers. These analyzers are then used to when
;; a new lexical analyzer is made for a language.
;;
;;; Lexical Types
;;
;; A lexical type defines a kind of lexical analyzer that will be
;; automatically generated from a grammar file based on some
;; predetermined attributes. For now these two attributes are
;; recognized :
;;
;; * matchdatatype : define the kind of lexical analyzer. That is :
;;
;; - regexp : define a regexp analyzer (see
;; `define-lex-regex-type-analyzer')
;;
;; - string : define a string analyzer (see
;; `define-lex-string-type-analyzer')
;;
;; - block : define a block type analyzer (see
;; `define-lex-block-type-analyzer')
;;
;; - sexp : define a sexp analyzer (see
;; `define-lex-sexp-type-analyzer')
;;
;; - keyword : define a keyword analyzer (see
;; `define-lex-keyword-type-analyzer')
;;
;; * syntax : define the syntax that matches a syntactic
;; expression. When syntax is matched the corresponding type
;; analyzer is entered and the resulting match data will be
;; interpreted based on the kind of analyzer (see matchdatatype
;; above).
;;
;; The following lexical types are predefined :
;;
;; +-------------+---------------+--------------------------------+
;; | type | matchdatatype | syntax |
;; +-------------+---------------+--------------------------------+
;; | punctuation | string | "\\(\\s.\\|\\s$\\|\\s'\\)+" |
;; | keyword | keyword | "\\(\\sw\\|\\s_\\)+" |
;; | symbol | regexp | "\\(\\sw\\|\\s_\\)+" |
;; | string | sexp | "\\s\"" |
;; | number | regexp | semantic-lex-number-expression |
;; | block | block | "\\s(\\|\\s)" |
;; +-------------+---------------+--------------------------------+
;;
;; In a grammar you must use a %type expression to automatically generate
;; the corresponding analyzers of that type.
;;
;; Here is an example to auto-generate punctuation analyzers
;; with 'matchdatatype and 'syntax predefined (see table above)
;;
;; %type <punctuation> ;; will auto-generate this kind of analyzers
;;
;; It is equivalent to write :
;;
;; %type <punctuation> syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
;;
;; ;; Some punctuations based on the type defines above
;;
;; %token <punctuation> NOT "!"
;; %token <punctuation> NOTEQ "!="
;; %token <punctuation> MOD "%"
;; %token <punctuation> MODEQ "%="
;;
;;; On the Semantic 1.x lexer
;;
;; In semantic 1.x, the lexical analyzer was an all purpose routine.
;; To boost efficiency, the analyzer is now a series of routines that
;; are constructed at build time into a single routine. This will
;; eliminate unneeded if statements to speed the lexer.
(require 'semantic-fw)
;;; Code:
;;; Compatibility
;;
(eval-and-compile
(if (not (fboundp 'with-syntax-table))
;; Copied from Emacs 21 for compatibility with released Emacses.
(defmacro with-syntax-table (table &rest body)
"With syntax table of current buffer set to a copy of TABLE, evaluate BODY.
The syntax table of the current buffer is saved, BODY is evaluated, and the
saved table is restored, even in case of an abnormal exit.
Value is what BODY returns."
(let ((old-table (make-symbol "table"))
(old-buffer (make-symbol "buffer")))
`(let ((,old-table (syntax-table))
(,old-buffer (current-buffer)))
(unwind-protect
(progn
(set-syntax-table (copy-syntax-table ,table))
,@body)
(save-current-buffer
(set-buffer ,old-buffer)
(set-syntax-table ,old-table))))))
))
;;; Semantic 2.x lexical analysis
;;
(defun semantic-lex-map-symbols (fun table &optional property)
"Call function FUN on every symbol in TABLE.
If optional PROPERTY is non-nil, call FUN only on every symbol which
as a PROPERTY value. FUN receives a symbol as argument."
(if (arrayp table)
(mapatoms
#'(lambda (symbol)
(if (or (null property) (get symbol property))
(funcall fun symbol)))
table)))
;;; Lexical keyword table handling.
;;
;; These keywords are keywords defined for using in a grammar with the
;; %keyword declaration, and are not keywords used in Emacs Lisp.
(defvar semantic-flex-keywords-obarray nil
"Buffer local keyword obarray for the lexical analyzer.
These keywords are matched explicitly, and converted into special symbols.")
(make-variable-buffer-local 'semantic-flex-keywords-obarray)
(defmacro semantic-lex-keyword-invalid (name)
"Signal that NAME is an invalid keyword name."
`(signal 'wrong-type-argument '(semantic-lex-keyword-p ,name)))
(defsubst semantic-lex-keyword-symbol (name)
"Return keyword symbol with NAME or nil if not found."
(and (arrayp semantic-flex-keywords-obarray)
(stringp name)
(intern-soft name semantic-flex-keywords-obarray)))
(defsubst semantic-lex-keyword-p (name)
"Return non-nil if a keyword with NAME exists in the keyword table.
Return nil otherwise."
(and (setq name (semantic-lex-keyword-symbol name))
(symbol-value name)))
(defsubst semantic-lex-keyword-set (name value)
"Set value of keyword with NAME to VALUE and return VALUE."
(set (intern name semantic-flex-keywords-obarray) value))
(defsubst semantic-lex-keyword-value (name)
"Return value of keyword with NAME.
Signal an error if a keyword with NAME does not exist."
(let ((keyword (semantic-lex-keyword-symbol name)))
(if keyword
(symbol-value keyword)
(semantic-lex-keyword-invalid name))))
(defsubst semantic-lex-keyword-put (name property value)
"For keyword with NAME, set its PROPERTY to VALUE."
(let ((keyword (semantic-lex-keyword-symbol name)))
(if keyword
(put keyword property value)
(semantic-lex-keyword-invalid name))))
(defsubst semantic-lex-keyword-get (name property)
"For keyword with NAME, return its PROPERTY value."
(let ((keyword (semantic-lex-keyword-symbol name)))
(if keyword
(get keyword property)
(semantic-lex-keyword-invalid name))))
(defun semantic-lex-make-keyword-table (specs &optional propspecs)
"Convert keyword SPECS into an obarray and return it.
SPECS must be a list of (NAME . TOKSYM) elements, where:
NAME is the name of the keyword symbol to define.
TOKSYM is the lexical token symbol of that keyword.
If optional argument PROPSPECS is non nil, then interpret it, and
apply those properties.
PROPSPECS must be a list of (NAME PROPERTY VALUE) elements."
;; Create the symbol hash table
(let ((semantic-flex-keywords-obarray (make-vector 13 0))
spec)
;; fill it with stuff
(while specs
(setq spec (car specs)
specs (cdr specs))
(semantic-lex-keyword-set (car spec) (cdr spec)))
;; Apply all properties
(while propspecs
(setq spec (car propspecs)
propspecs (cdr propspecs))
(semantic-lex-keyword-put (car spec) (nth 1 spec) (nth 2 spec)))
semantic-flex-keywords-obarray))
(defsubst semantic-lex-map-keywords (fun &optional property)
"Call function FUN on every lexical keyword.
If optional PROPERTY is non-nil, call FUN only on every keyword which
as a PROPERTY value. FUN receives a lexical keyword as argument."
(semantic-lex-map-symbols
fun semantic-flex-keywords-obarray property))
(defun semantic-lex-keywords (&optional property)
"Return a list of lexical keywords.
If optional PROPERTY is non-nil, return only keywords which have a
PROPERTY set."
(let (keywords)
(semantic-lex-map-keywords
#'(lambda (symbol) (setq keywords (cons symbol keywords)))
property)
keywords))
;;; Type table handling.
;;
;; The lexical type table manages types that occur in a grammar file
;; with the %type declaration. Types represent different syntaxes.
;; See code for `semantic-lex-preset-default-types' for the classic
;; types of syntax.
(defvar semantic-lex-types-obarray nil
"Buffer local types obarray for the lexical analyzer.")
(make-variable-buffer-local 'semantic-lex-types-obarray)
(defmacro semantic-lex-type-invalid (type)
"Signal that TYPE is an invalid lexical type name."
`(signal 'wrong-type-argument '(semantic-lex-type-p ,type)))
(defsubst semantic-lex-type-symbol (type)
"Return symbol with TYPE or nil if not found."
(and (arrayp semantic-lex-types-obarray)
(stringp type)
(intern-soft type semantic-lex-types-obarray)))
(defsubst semantic-lex-type-p (type)
"Return non-nil if a symbol with TYPE name exists."
(and (setq type (semantic-lex-type-symbol type))
(symbol-value type)))
(defsubst semantic-lex-type-set (type value)
"Set value of symbol with TYPE name to VALUE and return VALUE."
(set (intern type semantic-lex-types-obarray) value))
(defsubst semantic-lex-type-value (type &optional noerror)
"Return value of symbol with TYPE name.
If optional argument NOERROR is non-nil return nil if a symbol with
TYPE name does not exist. Otherwise signal an error."
(let ((sym (semantic-lex-type-symbol type)))
(if sym
(symbol-value sym)
(unless noerror
(semantic-lex-type-invalid type)))))
(defsubst semantic-lex-type-put (type property value &optional add)
"For symbol with TYPE name, set its PROPERTY to VALUE.
If optional argument ADD is non-nil, create a new symbol with TYPE
name if it does not already exist. Otherwise signal an error."
(let ((sym (semantic-lex-type-symbol type)))
(unless sym
(or add (semantic-lex-type-invalid type))
(semantic-lex-type-set type nil)
(setq sym (semantic-lex-type-symbol type)))
(put sym property value)))
(defsubst semantic-lex-type-get (type property &optional noerror)
"For symbol with TYPE name, return its PROPERTY value.
If optional argument NOERROR is non-nil return nil if a symbol with
TYPE name does not exist. Otherwise signal an error."
(let ((sym (semantic-lex-type-symbol type)))
(if sym
(get sym property)
(unless noerror
(semantic-lex-type-invalid type)))))
(defun semantic-lex-preset-default-types ()
"Install useful default properties for well known types."
(semantic-lex-type-put "punctuation" 'matchdatatype 'string t)
(semantic-lex-type-put "punctuation" 'syntax "\\(\\s.\\|\\s$\\|\\s'\\)+")
(semantic-lex-type-put "keyword" 'matchdatatype 'keyword t)
(semantic-lex-type-put "keyword" 'syntax "\\(\\sw\\|\\s_\\)+")
(semantic-lex-type-put "symbol" 'matchdatatype 'regexp t)
(semantic-lex-type-put "symbol" 'syntax "\\(\\sw\\|\\s_\\)+")
(semantic-lex-type-put "string" 'matchdatatype 'sexp t)
(semantic-lex-type-put "string" 'syntax "\\s\"")
(semantic-lex-type-put "number" 'matchdatatype 'regexp t)
(semantic-lex-type-put "number" 'syntax 'semantic-lex-number-expression)
(semantic-lex-type-put "block" 'matchdatatype 'block t)
(semantic-lex-type-put "block" 'syntax "\\s(\\|\\s)")
)
(defun semantic-lex-make-type-table (specs &optional propspecs)
"Convert type SPECS into an obarray and return it.
SPECS must be a list of (TYPE . TOKENS) elements, where:
TYPE is the name of the type symbol to define.
TOKENS is an list of (TOKSYM . MATCHER) elements, where:
TOKSYM is any lexical token symbol.
MATCHER is a string or regexp a text must match to be a such
lexical token.
If optional argument PROPSPECS is non nil, then interpret it, and
apply those properties.
PROPSPECS must be a list of (TYPE PROPERTY VALUE)."
;; Create the symbol hash table
(let* ((semantic-lex-types-obarray (make-vector 13 0))
spec type tokens token alist default)
;; fill it with stuff
(while specs
(setq spec (car specs)
specs (cdr specs)
type (car spec)
tokens (cdr spec)
default nil
alist nil)
(while tokens
(setq token (car tokens)
tokens (cdr tokens))
(if (cdr token)
(setq alist (cons token alist))
(setq token (car token))
(if default
(message
"*Warning* default value of <%s> tokens changed to %S, was %S"
type default token))
(setq default token)))
;; Ensure the default matching spec is the first one.
(semantic-lex-type-set type (cons default (nreverse alist))))
;; Install useful default types & properties
(semantic-lex-preset-default-types)
;; Apply all properties
(while propspecs
(setq spec (car propspecs)
propspecs (cdr propspecs))
;; Create the type if necessary.
(semantic-lex-type-put (car spec) (nth 1 spec) (nth 2 spec) t))
semantic-lex-types-obarray))
(defsubst semantic-lex-map-types (fun &optional property)
"Call function FUN on every lexical type.
If optional PROPERTY is non-nil, call FUN only on every type symbol
which as a PROPERTY value. FUN receives a type symbol as argument."
(semantic-lex-map-symbols
fun semantic-lex-types-obarray property))
(defun semantic-lex-types (&optional property)
"Return a list of lexical type symbols.
If optional PROPERTY is non-nil, return only type symbols which have
PROPERTY set."
(let (types)
(semantic-lex-map-types
#'(lambda (symbol) (setq types (cons symbol types)))
property)
types))
;;; Lexical Analyzer framework settings
;;
;;;###autoload
(defvar semantic-lex-analyzer 'semantic-flex
"The lexical analyzer used for a given buffer.
See `semantic-lex' for documentation.
For compatibility with Semantic 1.x it defaults to `semantic-flex'.")
(make-variable-buffer-local 'semantic-lex-analyzer)
(defvar semantic-lex-tokens
'(
(bol)
(charquote)
(close-paren)
(comment)
(newline)
(open-paren)
(punctuation)
(semantic-list)
(string)
(symbol)
(whitespace)
)
"An alist of of semantic token types.
As of December 2001 (semantic 1.4beta13), this variable is not used in
any code. The only use is to refer to the doc-string from elsewhere.
The key to this alist is the symbol representing token type that
\\[semantic-flex] returns. These are
- bol: Empty string matching a beginning of line.
This token is produced with
`semantic-lex-beginning-of-line'.
- charquote: String sequences that match `\\s\\+' regexp.
This token is produced with `semantic-lex-charquote'.
- close-paren: Characters that match `\\s)' regexp.
These are typically `)', `}', `]', etc.
This token is produced with
`semantic-lex-close-paren'.
- comment: A comment chunk. These token types are not
produced by default.
This token is produced with `semantic-lex-comments'.
Comments are ignored with `semantic-lex-ignore-comments'.
Comments are treated as whitespace with
`semantic-lex-comments-as-whitespace'.
- newline Characters matching `\\s-*\\(\n\\|\\s>\\)' regexp.
This token is produced with `semantic-lex-newline'.
- open-paren: Characters that match `\\s(' regexp.
These are typically `(', `{', `[', etc.
If `semantic-lex-paren-or-list' is used,
then `open-paren' is not usually generated unless
the `depth' argument to \\[semantic-lex] is
greater than 0.
This token is always produced if the analyzer
`semantic-lex-open-paren' is used.
- punctuation: Characters matching `{\\(\\s.\\|\\s$\\|\\s'\\)'
regexp.
This token is produced with `semantic-lex-punctuation'.
Always specify this analyzer after the comment
analyzer.
- semantic-list: String delimited by matching parenthesis, braces,
etc. that the lexer skipped over, because the
`depth' parameter to \\[semantic-flex] was not high
enough.
This token is produced with `semantic-lex-paren-or-list'.
- string: Quoted strings, i.e., string sequences that start
and end with characters matching `\\s\"'
regexp. The lexer relies on @code{forward-sexp} to
find the matching end.
This token is produced with `semantic-lex-string'.
- symbol: String sequences that match `\\(\\sw\\|\\s_\\)+'
regexp.
This token is produced with
`semantic-lex-symbol-or-keyword'. Always add this analyzer
after `semantic-lex-number', or other analyzers that
match its regular expression.
- whitespace: Characters that match `\\s-+' regexp.
This token is produced with `semantic-lex-whitespace'.")
(defvar semantic-lex-syntax-modifications nil
"Changes to the syntax table for this buffer.
These changes are active only while the buffer is being flexed.
This is a list where each element has the form:
(CHAR CLASS)
CHAR is the char passed to `modify-syntax-entry',
and CLASS is the string also passed to `modify-syntax-entry' to define
what syntax class CHAR has.")
(make-variable-buffer-local 'semantic-lex-syntax-modifications)
(defvar semantic-lex-syntax-table nil
"Syntax table used by lexical analysis.
See also `semantic-lex-syntax-modifications'.")
(make-variable-buffer-local 'semantic-lex-syntax-table)
(defvar semantic-lex-comment-regex nil
"Regular expression for identifying comment start during lexical analysis.
This may be automatically set when semantic initializes in a mode, but
may need to be overriden for some special languages.")
(make-variable-buffer-local 'semantic-lex-comment-regex)
(defvar semantic-lex-number-expression
;; This expression was written by David Ponce for Java, and copied
;; here for C and any other similar language.
(eval-when-compile
(concat "\\("
"\\<[0-9]+[.][0-9]+\\([eE][-+]?[0-9]+\\)?[fFdD]?\\>"
"\\|"
"\\<[0-9]+[.][eE][-+]?[0-9]+[fFdD]?\\>"
"\\|"
"\\<[0-9]+[.][fFdD]\\>"
"\\|"
"\\<[0-9]+[.]"
"\\|"
"[.][0-9]+\\([eE][-+]?[0-9]+\\)?[fFdD]?\\>"
"\\|"
"\\<[0-9]+[eE][-+]?[0-9]+[fFdD]?\\>"
"\\|"
"\\<0[xX][0-9a-fA-F]+[lL]?\\>"
"\\|"
"\\<[0-9]+[lLfFdD]?\\>"
"\\)"
))
"Regular expression for matching a number.
If this value is nil, no number extraction is done during lex.
This expression tries to match C and Java like numbers.
DECIMAL_LITERAL:
[1-9][0-9]*
;
HEX_LITERAL:
0[xX][0-9a-fA-F]+
;
OCTAL_LITERAL:
0[0-7]*
;
INTEGER_LITERAL:
<DECIMAL_LITERAL>[lL]?
| <HEX_LITERAL>[lL]?
| <OCTAL_LITERAL>[lL]?
;
EXPONENT:
[eE][+-]?[09]+
;
FLOATING_POINT_LITERAL:
[0-9]+[.][0-9]*<EXPONENT>?[fFdD]?
| [.][0-9]+<EXPONENT>?[fFdD]?
| [0-9]+<EXPONENT>[fFdD]?
| [0-9]+<EXPONENT>?[fFdD]
;")
(make-variable-buffer-local 'semantic-lex-number-expression)
(defvar semantic-lex-depth 0
"Default lexing depth.
This specifies how many lists to create tokens in.")
(make-variable-buffer-local 'semantic-lex-depth)
(defvar semantic-lex-unterminated-syntax-end-function
(lambda (syntax syntax-start lex-end) lex-end)
"Function called when unterminated syntax is encountered.
This should be set to one function. That function should take three
parameters. The SYNTAX, or type of syntax which is unterminated.
SYNTAX-START where the broken syntax begins.
LEX-END is where the lexical analysis was asked to end.
This function can be used for languages that can intelligently fix up
broken syntax, or the exit lexical analysis via `throw' or `signal'
when finding unterminated syntax.")
;;; Interactive testing commands
(defun semantic-lex-test (arg)
"Test the semantic lexer in the current buffer.
If universal argument ARG, then try the whole buffer."
(interactive "P")
(let* ((start (current-time))
(result (semantic-lex
(if arg (point-min) (point))
(point-max)))
(end (current-time)))
(message "Elapsed Time: %.2f seconds."
(semantic-elapsed-time start end))
(pop-to-buffer "*Lexer Output*")
(require 'pp)
(erase-buffer)
(insert (pp-to-string result))
(goto-char (point-min))
))
(defun semantic-lex-test-full-depth (arg)
"Test the semantic lexer in the current buffer parsing through lists.
Usually the lexer parses
If universal argument ARG, then try the whole buffer."
(interactive "P")
(let* ((start (current-time))
(result (semantic-lex
(if arg (point-min) (point))
(point-max)
100))
(end (current-time)))
(message "Elapsed Time: %.2f seconds."
(semantic-elapsed-time start end))
(pop-to-buffer "*Lexer Output*")
(require 'pp)
(erase-buffer)
(insert (pp-to-string result))
(goto-char (point-min))
))
(defun semantic-lex-test-region (beg end)
"Test the semantic lexer in the current buffer.
Analyze the area between BEG and END."
(interactive "r")
(let ((result (semantic-lex beg end)))
(pop-to-buffer "*Lexer Output*")
(require 'pp)
(erase-buffer)
(insert (pp-to-string result))
(goto-char (point-min))
))
(defvar semantic-lex-debug nil
"When non-nil, debug the local lexical analyzer.")
(defun semantic-lex-debug (arg)
"Debug the semantic lexer in the current buffer.
Argument ARG specifies of the analyze the whole buffer, or start at point.
While engaged, each token identified by the lexer will be highlighted
in the target buffer A description of the current token will be
displayed in the minibuffer. Press SPC to move to the next lexical token."
(interactive "P")
(require 'semantic-debug)
(let ((semantic-lex-debug t))
(semantic-lex-test arg)))
(defun semantic-lex-highlight-token (token)
"Highlight the lexical TOKEN.
TOKEN is a lexical token with a START And END position.
Return the overlay."
(let ((o (semantic-make-overlay (semantic-lex-token-start token)
(semantic-lex-token-end token))))
(semantic-overlay-put o 'face 'highlight)
o))
(defsubst semantic-lex-debug-break (token)
"Break during lexical analysis at TOKEN."
(when semantic-lex-debug
(let ((o nil))
(unwind-protect
(progn
(when token
(setq o (semantic-lex-highlight-token token)))
(semantic-read-event
(format "%S :: SPC - continue" token))
)
(when o
(semantic-overlay-delete o))))))
;;; Lexical analyzer creation
;;
;; Code for creating a lex function from lists of analyzers.
;;
;; A lexical analyzer is created from a list of individual analyzers.
;; Each individual analyzer specifies a single match, and code that
;; goes with it.
;;
;; Creation of an analyzer assembles these analyzers into a new function
;; with the behaviors of all the individual analyzers.
;;
(defmacro semantic-lex-one-token (analyzers)
"Calculate one token from the current buffer at point.
Uses locally bound variables from `define-lex'.
Argument ANALYZERS is the list of analyzers being used."
(cons 'cond (mapcar #'symbol-value analyzers)))
(defvar semantic-lex-end-point nil
"The end point as tracked through lexical functions.")
(defvar semantic-lex-current-depth nil
"The current depth as tracked through lexical functions.")
(defvar semantic-lex-maximum-depth nil
"The maximum depth of parenthisis as tracked through lexical functions.")
(defvar semantic-lex-token-stream nil
"The current token stream we are collecting.")
(defvar semantic-lex-analysis-bounds nil
"The bounds of the current analysis.")
(defvar semantic-lex-block-streams nil
"Streams of tokens inside collapsed blocks.
This is an alist of (ANCHOR . STREAM) elements where ANCHOR is the
start position of the block, and STREAM is the list of tokens in that
block.")
(defvar semantic-lex-reset-hooks nil
"List of hooks major-modes use to reset lexical analyzers.
Hooks are called with START and END values for the current lexical pass.
Should be set with `add-hook'specifying a LOCAL option.")
;; Stack of nested blocks.
(defvar semantic-lex-block-stack nil)
;;(defvar semantic-lex-timeout 5
;; "*Number of sections of lexing before giving up.")
;;;###autoload
(defmacro define-lex (name doc &rest analyzers)
"Create a new lexical analyzer with NAME.
DOC is a documentation string describing this analyzer.
ANALYZERS are small code snippets of analyzers to use when
building the new NAMED analyzer. Only use analyzers which
are written to be used in `define-lex'.
Each analyzer should be an analyzer created with `define-lex-analyzer'.
Note: The order in which analyzers are listed is important.
If two analyzers can match the same text, it is important to order the
analyzers so that the one you want to match first occurs first. For
example, it is good to put a numbe analyzer in front of a symbol
analyzer which might mistake a number for as a symbol."
`(defun ,name (start end &optional depth length)
,(concat doc "\nSee `semantic-lex' for more information.")
;; Make sure the state of block parsing starts over.
(setq semantic-lex-block-streams nil)
;; Allow specialty reset items.
(run-hook-with-args 'semantic-lex-reset-hooks start end)
;; Lexing state.
(let* (;(starttime (current-time))
(starting-position (point))
(semantic-lex-token-stream nil)
(semantic-lex-block-stack nil)
(tmp-start start)
(semantic-lex-end-point start)
(semantic-lex-current-depth 0)
;; Use the default depth when not specified.
(semantic-lex-maximum-depth
(or depth semantic-lex-depth))
;; Bounds needed for unterminated syntax
(semantic-lex-analysis-bounds (cons start end))
;; This entry prevents text properties from
;; confusing our lexical analysis. See Emacs 22 (CVS)
;; version of C++ mode with template hack text properties.
(parse-sexp-lookup-properties nil)
)
;; Maybe REMOVE THIS LATER.
;; Trying to find incremental parser bug.
(when (> end (point-max))
(error ,(format "%s: end (%%d) > point-max (%%d)" name)
end (point-max)))
(with-syntax-table semantic-lex-syntax-table
(goto-char start)
(while (and (< (point) end)
(or (not length)
(<= (length semantic-lex-token-stream) length)))
(semantic-lex-one-token ,analyzers)
(when (eq semantic-lex-end-point tmp-start)
(error ,(format "%s: endless loop at %%d, after %%S" name)
tmp-start (car semantic-lex-token-stream)))
(setq tmp-start semantic-lex-end-point)
(goto-char semantic-lex-end-point)
;;(when (> (semantic-elapsed-time starttime (current-time))
;; semantic-lex-timeout)
;; (error "Timeout during lex at char %d" (point)))
(semantic-throw-on-input 'lex)
(semantic-lex-debug-break (car semantic-lex-token-stream))
))
;; Check that there is no unterminated block.
(when semantic-lex-block-stack
(let* ((last (pop semantic-lex-block-stack))
(blk last))
(while blk
(message
,(format "%s: `%%s' block from %%S is unterminated" name)
(car blk) (cadr blk))
(setq blk (pop semantic-lex-block-stack)))
(semantic-lex-unterminated-syntax-detected (car last))))
;; Return to where we started.
;; Do not wrap in protective stuff so that if there is an error
;; thrown, the user knows where.
(goto-char starting-position)
;; Return the token stream
(nreverse semantic-lex-token-stream))))
;;; Collapsed block tokens delimited by any tokens.
;;
(defun semantic-lex-start-block (syntax)
"Mark the last read token as the beginning of a SYNTAX block."
(if (or (not semantic-lex-maximum-depth)
(< semantic-lex-current-depth semantic-lex-maximum-depth))
(setq semantic-lex-current-depth (1+ semantic-lex-current-depth))
(push (list syntax (car semantic-lex-token-stream))
semantic-lex-block-stack)))
(defun semantic-lex-end-block (syntax)
"Process the end of a previously marked SYNTAX block.
That is, collapse the tokens inside that block, including the
beginning and end of block tokens, into a high level block token of
class SYNTAX.
The token at beginning of block is the one marked by a previous call
to `semantic-lex-start-block'. The current token is the end of block.
The collapsed tokens are saved in `semantic-lex-block-streams'."
(if (null semantic-lex-block-stack)
(setq semantic-lex-current-depth (1- semantic-lex-current-depth))
(let* ((stream semantic-lex-token-stream)
(blk (pop semantic-lex-block-stack))
(bstream (cdr blk))
(first (car bstream))
(last (pop stream)) ;; The current token mark the EOBLK
tok)
(if (not (eq (car blk) syntax))
;; SYNTAX doesn't match the syntax of the current block in
;; the stack. So we encountered the end of the SYNTAX block
;; before the end of the current one in the stack which is
;; signaled unterminated.
(semantic-lex-unterminated-syntax-detected (car blk))
;; Move tokens found inside the block from the main stream
;; into a separate block stream.
(while (and stream (not (eq (setq tok (pop stream)) first)))
(push tok bstream))
;; The token marked as beginning of block was not encountered.
;; This should not happen!
(or (eq tok first)
(error "Token %S not found at beginning of block `%s'"
first syntax))
;; Save the block stream for future reuse, to avoid to redo
;; the lexical analysis of the block content!
;; Anchor the block stream with its start position, so we can
;; use: (cdr (assq start semantic-lex-block-streams)) to
;; quickly retrieve the lexical stream associated to a block.
(setcar blk (semantic-lex-token-start first))
(setcdr blk (nreverse bstream))
(push blk semantic-lex-block-streams)
;; In the main stream, replace the tokens inside the block by
;; a high level block token of class SYNTAX.
(setq semantic-lex-token-stream stream)
(semantic-lex-push-token
(semantic-lex-token
syntax (car blk) (semantic-lex-token-end last)))
))))
;;; Lexical token API
;;
;; Functions for accessing parts of a token. Use these functions
;; instead of accessing the list structure directly because the
;; contents of the lexical may change.
;;
(defmacro semantic-lex-token (symbol start end &optional str)
"Create a lexical token.
SYMBOL is a symbol representing the class of syntax found.
START and END define the bounds of the token in the current buffer.
Optional STR is the string for the token iff the the bounds
in the buffer do not cover the string they represent. (As from
macro expansion.)"
;; This if statement checks the existance of a STR argument at
;; compile time, where STR is some symbol or constant. If the
;; variable STr (runtime) is nil, this will make an incorrect decision.
;;
;; It is like this to maintain the original speed of the compiled
;; code.
(if str
`(cons ,symbol (cons ,str (cons ,start ,end)))
`(cons ,symbol (cons ,start ,end))))
(defun semantic-lex-token-p (thing)
"Return non-nil if THING is a semantic lex token.
This is an exhaustively robust check."
(and (consp thing)
(symbolp (car thing))
(or (and (numberp (nth 1 thing))
(numberp (nthcdr 2 thing)))
(and (stringp (nth 1 thing))
(numberp (nth 2 thing))
(numberp (nthcdr 3 thing)))
))
)
(defun semantic-lex-token-with-text-p (thing)
"Return non-nil if THING is a semantic lex token.
This is an exhaustively robust check."
(and (consp thing)
(symbolp (car thing))
(= (length thing) 4)
(stringp (nth 1 thing))
(numberp (nth 2 thing))
(numberp (nth 3 thing)))
)
(defun semantic-lex-token-without-text-p (thing)
"Return non-nil if THING is a semantic lex token.
This is an exhaustively robust check."
(and (consp thing)
(symbolp (car thing))
(= (length thing) 3)
(numberp (nth 1 thing))
(numberp (nth 2 thing)))
)
(defun semantic-lex-expand-block-specs (specs)
"Expand block specifications SPECS into a Lisp form.
SPECS is a list of (BLOCK BEGIN END) elements where BLOCK, BEGIN, and
END are token class symbols that indicate to produce one collapsed
BLOCK token from tokens found between BEGIN and END ones.
BLOCK must be a non-nil symbol, and at least one of the BEGIN or END
symbols must be non-nil too.
When BEGIN is non-nil, generate a call to `semantic-lex-start-block'
when a BEGIN token class is encountered.
When END is non-nil, generate a call to `semantic-lex-end-block' when
an END token class is encountered."
(let ((class (make-symbol "class"))
(form nil))
(dolist (spec specs)
(when (car spec)
(when (nth 1 spec)
(push `((eq ',(nth 1 spec) ,class)
(semantic-lex-start-block ',(car spec)))
form))
(when (nth 2 spec)
(push `((eq ',(nth 2 spec) ,class)
(semantic-lex-end-block ',(car spec)))
form))))
(when form
`((let ((,class (semantic-lex-token-class
(car semantic-lex-token-stream))))
(cond ,@(nreverse form))))
)))
(defmacro semantic-lex-push-token (token &rest blockspecs)
"Push TOKEN in the lexical analyzer token stream.
Return the lexical analysis current end point.
If optional arguments BLOCKSPECS is non-nil, it specifies to process
collapsed block tokens. See `semantic-lex-expand-block-specs' for
more details.