source: project/release/5/slib-prec/trunk/slib-prec-grammar.scm @ 38598

Last change on this file since 38598 was 38598, checked in by Kon Lovett, 10 months ago

hide *output-grammar*, ... behind active-grammar

File size: 2.5 KB
Line 
1;;;; slib-prec-grammar.scm  -*- Scheme -*-
2;;;; Kon Lovett, Apr '20
3;;;; Kon Lovett, Apr '18
4
5(module slib-prec-grammar
6
7(;export
8  ;
9  active-grammar
10  active-grammar-set!
11  read-syntax-setup!
12  ;
13  make-grammar
14  grammar?
15  grammar-name
16  grammar-reader
17  grammar-read-tab
18  grammar-writer
19  grammar-write-tab
20  ;
21  grammar-id-name-set!
22  grammar-id-name
23  list-of-grammar-ids
24  count-of-grammar-ids
25  clear-grammar-ids
26  grammar-id-name-setup!
27  ;
28  defgrammar
29  get-grammar
30  list-of-grammars
31  count-of-grammars
32  get-grammar/id
33  clear-grammars
34  ;
35  read-sexp
36  write-sexp
37  print-using-grammar
38  flush-input-whitespace)
39
40(import scheme)
41(import (chicken base))
42(import (chicken type))
43(import slib-prec)
44
45;;;
46
47(define mod modulo)
48
49(include "slib-compat")
50
51(define (math:error . args) (apply slib:error 'math: args))
52(define (math:warn . args) (apply slib:warn 'math: args))
53(define (math:exit b) #;(cleanup-handlers!) (slib:error "error in math system"))
54
55(include "slib-grammar")
56
57(define *input-grammar*)
58(define *output-grammar*)
59(define *echo-grammar*)
60
61(include "output-grammars")
62(include "input-grammars")
63
64(defgrammar 'null
65  (make-grammar
66    'null
67                (lambda (grm) (math:error 'cannot-read-null-grammar))
68                #f
69                (lambda (sexp grm) #t)
70                #f))
71
72(defgrammar 'scheme
73  (make-grammar
74    'scheme
75                (lambda (grm) (read))
76                #f
77                (lambda (sexp grm) (write sexp) (force-output))
78                #f))
79
80;;; Establish autoload for PRETTY-PRINT.
81(defgrammar 'schemepretty
82  (make-grammar
83    'schemepretty
84    (lambda (grm)
85      (read) )
86    #f
87    (lambda (sexp grm)
88      (import (chicken pretty-print))
89      (pretty-print sexp)
90      (force-output) )
91    #f))
92
93(define (active-grammar-set! input output #!optional (echo 'null))
94  (set! *input-grammar* (get-grammar input))
95  (set! *output-grammar* output)
96  (set! *echo-grammar* echo) )
97
98(define (active-grammar) (values *input-grammar* *output-grammar* *echo-grammar*))
99
100;;
101
102(define (read-syntax-setup!)
103  (import (only (chicken base) parentheses-synonyms))
104  (import (only (chicken read-syntax) set-sharp-read-syntax! set-parameterized-read-syntax!))
105  (parentheses-synonyms #f)
106  (set-sharp-read-syntax! #\{
107    (lambda (port)
108      (read-sexp/delim (get-grammar/id 0) #\} port)))
109  (set-parameterized-read-syntax! #\{
110    (lambda (port id)
111      (read-sexp/delim (get-grammar/id id) #\} port))) )
112
113;;
114
115;init by id-# grammar lookup
116(define-constant DEFAULT-GRAMMARS '(
117  standard disp2d tex
118  null scheme schemepretty))
119(grammar-id-name-setup! DEFAULT-GRAMMARS)
120
121) ;slib-prec-grammar
Note: See TracBrowser for help on using the repository browser.