대부분의 구문 분석 문제와 마찬가지로 입력 형식의 요소를 가장 잘 설명하는 문법을 작성하려고합니다.
[comma ending value-chars qmark quoted-chars value header row]
일부 동사 :
[row-feed emit-value]
그리고 수술 명사 : 나는 아마도 그것을 무너 뜨리는 수도있을 것 같군요
[current chunk current-row width]
이 경우, 우리는 명사가 조금 더 있지만 작업하기에 충분합니다. 첫째, 기초 :
comma: ","
ending: "^/"
qmark: {"}
value-chars: complement charset reduce [qmark comma ending]
quoted-chars: complement charset reduce [qmark]
이제 가치 구조. 우리가 그들을 찾을 인용 값은 유효한 문자 나 따옴표의 덩어리에서 구축됩니다
이
delimiter
은 각 행의 시작 부분에
ending
로 설정하는 것으로
current: chunk: none
quoted-value: [
qmark (current: copy "")
any [
copy chunk some quoted-chars (append current chunk)
|
qmark qmark (append current qmark)
]
qmark
]
value: [
copy current some value-chars
| quoted-value
]
emit-value: [
(
delimiter: comma
append current-row current
)
]
emit-none: [
(
delimiter: comma
append current-row none
)
]
후 즉시 우리가 값을 통과 comma
로 변경되었습니다.따라서 입력 행은 [ending value any [comma value]]
으로 정의됩니다. 남아
모든 문서 구조를 정의하는 것입니다 :
current-row: none
row-feed: [
(
delimiter: ending
append/only out current-row: copy []
)
]
width: none
header: [
(out: copy [])
row-feed any [
value comma
emit-value
]
value body: ending :body
emit-value
(width: length? current-row)
]
row: [
row-feed width [
delimiter [
value emit-value
| emit-none
]
]
]
if parse/all stream [header some row opt ending][out]
이 모든 단어를 보호하기 위해 그것을 랩을하고, 당신은 :
REBOL [
Title: "CSV Parser"
Date: 19-Nov-2012
Author: "Christopher Ross-Gill"
]
parse-csv: use [
comma ending delimiter value-chars qmark quoted-chars
value quoted-value header row
row-feed emit-value emit-none
out current current-row width
][
comma: ","
ending: "^/"
qmark: {"}
value-chars: complement charset reduce [qmark comma ending]
quoted-chars: complement charset reduce [qmark]
current: none
quoted-value: use [chunk][
[
qmark (current: copy "")
any [
copy chunk some quoted-chars (append current chunk)
|
qmark qmark (append current qmark)
]
qmark
]
]
value: [
copy current some value-chars
| quoted-value
]
current-row: none
row-feed: [
(
delimiter: ending
append/only out current-row: copy []
)
]
emit-value: [
(
delimiter: comma
append current-row current
)
]
emit-none: [
(
delimiter: comma
append current-row none
)
]
width: none
header: [
(out: copy [])
row-feed any [
value comma
emit-value
]
value body: ending :body
emit-value
(width: length? current-row)
]
row: [
opt ending end break
|
row-feed width [
delimiter [
value emit-value
| emit-none
]
]
]
func [stream [string!]][
if parse/all stream [header some row][out]
]
]
내가받은 적이있는 이상한 데이터에 대해 (지금까지) 생각한 대답에 대한 환상적인 응답 시간! – HostileFork