Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:38:30+00:00 2026-06-14T13:38:30+00:00

I’m trying to use PARSE to turn a CSV line into a Rebol block.

  • 0

I’m trying to use PARSE to turn a CSV line into a Rebol block. Easy enough to write in open code, but as with other questions I am trying to learn what the dialect can do without that.

So if a line says:

"Look, that's ""MR. Fork"" to you!",Hostile Fork,,http://hostilefork.com

Then I want the block:

[{Look, that's "MR. Fork" to you!} {Hostile Fork} none {http://hostilefork.com}]

Issues to notice:

  • Embedded quotes in CSV strings are indicated with ""
  • Commas can be inside quotes and hence part of the literal, not a column separator
  • Adjacent column-separating commas indicate an empty field
  • Strings that don’t contain quotes or commas can appear without quotes
  • For the moment we can keep things like http://rebol.com as STRING! instead of LOADing them into types such as URL!

To make it more uniform, the first thing I do is append a comma to the input line. Then I have a column-rule which captures a single column terminated by a comma…which may either be in quotes or not.

I know how many columns there should be due to the header line, so the code then says:

unless parse line compose [(column-count) column-rule] [
    print rejoin [{Expected } column-count { columns.}]
]

But I’m a bit stuck on writing column-rule. I need a way in the dialect to express “Once you find a quote, keep skipping quote pairs until you find a quote standing all on its own.” What’s a good way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:38:32+00:00Added an answer on June 14, 2026 at 1:38 pm

    As with most parse problems, I try to build a grammar that best describes the elements of the input format.

    In this case, we have nouns:

    [comma ending value-chars qmark quoted-chars value header row]
    

    Some verbs:

    [row-feed emit-value]
    

    And the operative nouns:

    [current chunk current-row width]
    

    I suppose I could possibly break it down a little more, but is enough to work with. First, the foundation:

    comma: ","
    ending: "^/"
    qmark: {"}
    value-chars: complement charset reduce [qmark comma ending]
    quoted-chars: complement charset reduce [qmark]
    

    Now the value structure. Quoted values are built up from chunks of valid chars or quotes as we find them:

    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
        )
    ]
    

    Note that delimiter is set to ending at the beginning of each row, then changed to comma as soon as we pass a value. Thus, an input row is defined as [ending value any [comma value]].

    All that remains is to define the document structure:

    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]
    

    Wrap it up to shield all those words, and you have:

    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]
        ]
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.