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
|
#! /bin/bash
#|
exec mzscheme -qu "$0" ${1+"$@"}
|#
;; I'm duplicating the code from parser.ss and hacking it.
(module parser_fullnames mzscheme
;; Import the parser and lexer generators.
(require (lib "yacc.ss" "parser-tools")
(lib "lex.ss" "parser-tools")
(prefix : (lib "lex-sre.ss" "parser-tools"))
(lib "list.ss")
;(lib "iu-match.ss")
"iu-match.ss"
)
; (provide (all-defined))
(require (lib "pretty.ss"))
(define-empty-tokens op-tokens
(LeftParen RightParen Comma SemiColon Colon
EOF ))
(define-tokens value-tokens (NAME NUM))
(define-lex-abbrevs (lower-letter (:/ "a" "z"))
(upper-letter (:/ #\A #\Z))
(digit (:/ "0" "9"))
(digits (:* digit))
(letter (:or lower-letter upper-letter digit "." "'" "-" "/"))
(name (:seq (:+ letter) (:* (:seq "_" (:* letter)))))
(number (:seq digits "." digits (:? (:seq "E-" digits))))
)
(define ws-lex
(lexer-src-pos
[(eof) 'EOF]
;; Ignore all whitespace:
[(:or #\tab #\space #\newline #\return) (return-without-pos (ws-lex input-port))]
["(" 'LeftParen]
[")" 'RightParen]
["," 'Comma] [";" 'SemiColon] [":" 'Colon]
[(:seq "'silva_" name "'")
(begin ;(printf "TEST ~a\n" (substring lexeme 7 (sub1 (string-length lexeme))))
(token-NAME (substring lexeme 7 (sub1 (string-length lexeme))))
)]
[(:seq "silva_" name) (token-NAME (substring lexeme 6 (string-length lexeme)))]
;[number (token-NUM (string->number lexeme))]
[number (token-NUM lexeme)]
))
(define (format-pos pos)
(if (position-line pos)
(format "line ~a:~a" (position-line pos) (position-col pos))
(format "char ~a" (position-offset pos))))
(define ws-parse
(parser
(src-pos)
(start wholefile)
(end EOF)
(tokens value-tokens op-tokens)
(error (lambda (a b c start end)
(printf "PARSE ERROR: after ~a token, ~a~a.\n"
(if a "valid" "invalid") b
(if c (format " carrying value ~s" c) ""))
(printf " Located between ~a and ~a.\n"
(format-pos start) (format-pos end))))
;; Precedence:
;(precs )
;(debug "_parser.log")
(grammar
(wholefile [(tree SemiColon) $1]
;; ACK making semicolon and final tag optional!!
[(tree) $1]
[(LeftParen nodes+ RightParen) (cons "0.0" $2)]
)
(numtag [(Colon NUM) $2])
(tree [(NAME numtag) (list $2 $1)]
[(LeftParen nodes+ RightParen numtag) (cons $4 $2)])
(nodes+ [(tree) (list $1)]
[(tree Comma nodes+) (cons $1 $3)])
)))
;; ================================================================================
(define (ws-parse-file f)
(let ([p (open-input-file f)])
(let ([res (ws-parse-port p)])
(close-input-port p)
res)))
(define (ws-parse-port ip)
(port-count-lines! ip)
;; cdr
(ws-parse (lambda () (flatten (ws-lex ip))))
;(position-token-token (ws-lex ip))
)
(define (flatten pt)
;(printf " ")
(let loop ((x pt))
(if (position-token? (position-token-token x))
(begin (error 'flatten "No nested position-tokens!")
(loop (position-token-token x)))
x)))
(define allargs (vector->list (current-command-line-arguments)))
(define pretty? #t)
(define (main filename)
#;
(if pretty?
(pretty-print (ws-parse-file filename))
(begin (write (ws-parse-file filename))
(newline)
))
(define table '())
(define counter 1)
(define (clean-name n)
(define matches (regexp-match #rx".*__(.*)" n))
(define cleaned (car (reverse matches)))
;(printf "MATCHES ~a\n" matches)
(set! table `((,counter ,cleaned) . ,table))
(set! counter (+ 1 counter))
;(sub1 counter)
;; THIS IS ALL I CHANGED:
cleaned
)
(with-output-to-file (string-append filename ".nameonly")
(lambda ()
(let loop ((x (ws-parse-file filename)))
(match x
;; A leaf:
[(,dist ,name) (guard (string? name))
(printf "~a:~a" (clean-name name) dist)]
[(,dist ,children ...)
(printf "(")
(let inner ((ls children))
(cond
[(null? ls) (void)]
[(null? (cdr ls)) (loop (car ls))]
[else (loop (car ls)) (printf ",")
(inner (cdr ls))]))
(printf "):~a" dist)]
))
(printf ";\n")))
(with-output-to-file (string-append filename ".table")
(lambda ()
(for-each
(lambda (ls) (printf "~a ~a\n" (car ls) (cadr ls)))
(reverse table))))
)
(print-graph #t)
(print-vector-length #f)
;; Here's our script invocation:
;; When run in --persist mode we run in a loop.
(if (null? allargs)
(error 'wsparse "No filename provided...")
(main (car allargs)))
(printf "Done writing output files.\n")
;(exit 0)
) ;; End module.
|