Racket 词法分析器 - return 缺点列表
Racket lexer - return list of cons
我是 Racket 的新手,但对此非常兴奋。我一直在为 WWW-Authenticate headers 编写一个简单的词法分析器。我对词法分析感觉很好,但现在我想更改我的输出。
#lang racket
(require parser-tools/lex)
(require (prefix-in : parser-tools/lex-sre))
(define in (open-input-string "MsRtcOAuth href=\"https://foo.com/WebTicket/oauthtoken\",grant_type=\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\", Bearer trusted_issuers=\"\", client_id=\"00000004-0000-0ff1-ce00-000000000000\""))
(define key-lexer
(lexer
;anything not an "="
[(:* (char-complement #\=))
;=>
(cons `(KEY, lexeme)
(equals-lexer input-port))]
;eof
[(eof) '()]))
(define equals-lexer
(lexer
[#\=
;=>
(value-lexer input-port)]
;eof
[(eof) '()]))
(define value-lexer
(lexer
;values are anything between two " "
[(concatenation #\" (:* (char-complement #\")) #\")
;=>
(cons `(VAL, lexeme)
(comma-lexer input-port))]
;eof
[(eof) '()]))
(define comma-lexer
(lexer
[(concatenation (:* whitespace) #\, (:* whitespace))
;=>
(key-lexer input-port)]
;eof
[(eof) '()]))
(key-lexer in)
现在,输出如下所示:
'((KEY "MsRtcOAuth href")
(VAL "\"https://foo.com/WebTicket/oauthtoken\"")
(KEY "grant_type")
(VAL "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
(KEY "Bearer trusted_issuers")
(VAL "\"\"")
(KEY "client_id")
(VAL "\"00000004-0000-0ff1-ce00-000000000000\""))
我更喜欢的是成对列表,类似于:
(("MsRtcOAuth href" . "\"https://foo.com/WebTicket/oauthtoken\"")
("grant_type" . "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"") etc...
非常感谢任何帮助或指点。谢谢!
这里有一种方法可以将您已有的变成您想要的:
(define (prettify key-val-pairs)
(match key-val-pairs
[(list (list 'KEY key) (list 'VAL val) more ...)
(cons (list key val)
(prettify more))]
[_ key-val-pairs]))
(prettify
'((KEY "MsRtcOAuth href")
(VAL "\"https://foo.com/WebTicket/oauthtoken\"")
(KEY "grant_type")
(VAL "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
(KEY "Bearer trusted_issuers")
(VAL "\"\"")
(KEY "client_id")
(VAL "\"00000004-0000-0ff1-ce00-000000000000\"")))
输出:
'(("MsRtcOAuth href" "\"https://foo.com/WebTicket/oauthtoken\"")
("grant_type" "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
("Bearer trusted_issuers" "\"\"")
("client_id" "\"00000004-0000-0ff1-ce00-000000000000\""))
我是 Racket 的新手,但对此非常兴奋。我一直在为 WWW-Authenticate headers 编写一个简单的词法分析器。我对词法分析感觉很好,但现在我想更改我的输出。
#lang racket
(require parser-tools/lex)
(require (prefix-in : parser-tools/lex-sre))
(define in (open-input-string "MsRtcOAuth href=\"https://foo.com/WebTicket/oauthtoken\",grant_type=\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\", Bearer trusted_issuers=\"\", client_id=\"00000004-0000-0ff1-ce00-000000000000\""))
(define key-lexer
(lexer
;anything not an "="
[(:* (char-complement #\=))
;=>
(cons `(KEY, lexeme)
(equals-lexer input-port))]
;eof
[(eof) '()]))
(define equals-lexer
(lexer
[#\=
;=>
(value-lexer input-port)]
;eof
[(eof) '()]))
(define value-lexer
(lexer
;values are anything between two " "
[(concatenation #\" (:* (char-complement #\")) #\")
;=>
(cons `(VAL, lexeme)
(comma-lexer input-port))]
;eof
[(eof) '()]))
(define comma-lexer
(lexer
[(concatenation (:* whitespace) #\, (:* whitespace))
;=>
(key-lexer input-port)]
;eof
[(eof) '()]))
(key-lexer in)
现在,输出如下所示:
'((KEY "MsRtcOAuth href")
(VAL "\"https://foo.com/WebTicket/oauthtoken\"")
(KEY "grant_type")
(VAL "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
(KEY "Bearer trusted_issuers")
(VAL "\"\"")
(KEY "client_id")
(VAL "\"00000004-0000-0ff1-ce00-000000000000\""))
我更喜欢的是成对列表,类似于:
(("MsRtcOAuth href" . "\"https://foo.com/WebTicket/oauthtoken\"")
("grant_type" . "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"") etc...
非常感谢任何帮助或指点。谢谢!
这里有一种方法可以将您已有的变成您想要的:
(define (prettify key-val-pairs)
(match key-val-pairs
[(list (list 'KEY key) (list 'VAL val) more ...)
(cons (list key val)
(prettify more))]
[_ key-val-pairs]))
(prettify
'((KEY "MsRtcOAuth href")
(VAL "\"https://foo.com/WebTicket/oauthtoken\"")
(KEY "grant_type")
(VAL "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
(KEY "Bearer trusted_issuers")
(VAL "\"\"")
(KEY "client_id")
(VAL "\"00000004-0000-0ff1-ce00-000000000000\"")))
输出:
'(("MsRtcOAuth href" "\"https://foo.com/WebTicket/oauthtoken\"")
("grant_type" "\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\"")
("Bearer trusted_issuers" "\"\"")
("client_id" "\"00000004-0000-0ff1-ce00-000000000000\""))