File: keywords.rkt

package info (click to toggle)
racket-mode 20250711~git.8a80578-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,024 kB
  • sloc: lisp: 17,215; makefile: 106
file content (103 lines) | stat: -rw-r--r-- 3,576 bytes parent folder | download | duplicates (2)
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
;; Copyright (c) 2013-2022 by Greg Hendershott.
;; SPDX-License-Identifier: GPL-3.0-or-later

#lang typed/racket/no-check

;; Generate lists for Racket keywords, builtins, and types.
;;
;; The question of what is a "keyword" and a "builtin" is not so
;; simple in Racket:
;;
;; 1. The distinction between the two is squishy, and from one point
;; of view Racket has 1400+ "primitives" (!).
;;
;; 2. As for "builtins", there are many, many "batteries included"
;; libraries in the main distribution. Where to draw the line?
;;
;; 3. More fundamentally, Racket is a language for making languages.
;; Ultimately the only way to be 100% correct is to do something
;; "live" with namespace-mapped-symbols. But I don't see that as
;; performant for Emacs font-lock.
;;
;; Here I'm saying that:
;;
;; (a) "keywords" are syntax (only) from racket/base
;;
;; (b) "builtins" are everything else provided by #lang racket and
;; #lang typed/racket (except the capitalized Types from typed/racket
;; go into their own list). Plus for modern macros, racket/syntax and
;; a few items from syntax/parse (but not its syntax classes, because
;; `id` and `str` are too "generic" and too likely to be user program
;; identifiers).
;;
;; Is that somewhat arbitrary? Hell yes. It's my least-worst,
;; practical idea for now. Also, IMHO it's an improvement over getting
;; pull requests to add people's favorites, a few at a time. At least
;; this way is consistent, and can be regenerated programatically as
;; Racket evolves.

(define (symbol<=? a b)
  (string<=? (symbol->string a) (symbol->string b)))

(define (exports mod #:only-stx? [only-stx? #f])
  (define (ids phases)
    (for*/list ([phase phases]
                [item (cdr phase)])
      (car item)))
  (define-values (vars stxs) (module->exports mod))
  (sort (remove-duplicates (append (ids stxs)
                                   (if only-stx? '() (ids vars)))
                           eq?)
        symbol<=?))

(define (subtract xs ys)
  (for*/list ([x xs] #:when (not (memq x ys))) x))

(define base-stx (exports 'racket/base #:only-stx? #t))

(define rkt      (append (exports 'racket)
                         (exports 'racket/syntax)
                         '(syntax-parse syntax-parser define-simple-macro)))
(define rkt+     (subtract rkt base-stx))

(define tr       (exports 'typed/racket))
(define tr+      (subtract tr rkt)) ;This includes Types, too

(define Types    (for/list ([x tr+]
                            #:when (char-upper-case? (string-ref (symbol->string x) 0)))
                   x))

;;; The final lists

(define keywords base-stx)

(define builtins
  (sort (subtract (remove-duplicates (append rkt+
                                             (subtract tr+ Types))
                                     eq?)
                  base-stx)
        symbol<=?))

;; So many builtins, Emacs gives "regexp too long" error, so split into two:
(define-values (builtins1 builtins2)
  (let ([mid (/ (length builtins) 2)])
    (for/fold ([xs '()]
               [ys '()])
              ([x builtins]
               [i (in-naturals)])
      (cond [(< i mid) (values (cons x xs) ys)]
            [else      (values xs (cons x ys))]))))

(define types Types)

;;; Print

(define (prn xs)
  (pretty-print (map symbol->string (sort xs symbol<=?))))

;; Enter each submodule to print a quoted list of symbols, then copy
;; and paste each list into racket-keywords-and-builtins.el.
(module+ types    (prn types))
(module+ keywords (prn keywords))
(module+ builtins1 (prn builtins1))
(module+ builtins2 (prn builtins2))