Skip to content

Commit

Permalink
fix CSV example
Browse files Browse the repository at this point in the history
importlib.metadata is import Python csv module which interfere with this
example. This changes the name of the module to `csvlang.py`
  • Loading branch information
igordejanovic committed Nov 24, 2023
1 parent 4505261 commit 087c3c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
34 changes: 17 additions & 17 deletions docs/tutorials/csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and easy to understand so it it a good starter for learning Arpeggio.

## The grammar

Let's start first by creating a python module called `csv.py`.
Let's start first by creating a python module called `csvlang.py`.

Now, let's define CSV grammar.

Expand Down Expand Up @@ -63,7 +63,7 @@ Now, let's define CSV grammar.
be escaped by doubling it (`""`).


The whole content of the `csv.py` file until now should be:
The whole content of the `csvlang.py` file until now should be:

from arpeggio import *
from arpeggio import RegExMatch as _
Expand All @@ -86,7 +86,7 @@ so we will use `ws` parameter in parser construction to redefine what is
considered as whitespace. You can find more information
[here](../configuration.md#white-space-handling).

After the grammar in `csv.py` instantiate the parser:
After the grammar in `csvlang.py` instantiate the parser:

parser = ParserPython(csvfile, ws='\t ')

Expand All @@ -108,7 +108,7 @@ Create file `test_data.csv` with the following content:
Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "34312.7"
Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343486.12"

In `csv.py` file write:
In `csvlang.py` file write:

```python
test_data = open('test_data.csv', 'r').read()
Expand All @@ -119,12 +119,12 @@ parse_tree = parser.parse(test_data)
`test_data` is Python string containing test CSV data from the file. Calling
`parser.parse` on the data will produce the [parse tree](../parse_trees.md).

If you run `csv.py` module, and there are no syntax errors in the `test_data.csv`
file, `parse_tree` will be a reference to [parse tree](../parse_trees.md) of
the test CSV data.
If you run `csvlang.py` module, and there are no syntax errors in the
`test_data.csv` file, `parse_tree` will be a reference to [parse
tree](../parse_trees.md) of the test CSV data.

```bash
$ python csv.py
$ python csvlang.py
```

**Congratulations!! You have successfully parsed CSV file.**
Expand Down Expand Up @@ -156,7 +156,7 @@ definition.
We shall repeat the process above but we shall encode rules in PEG.
We shall use clean PEG variant (`arpeggio.cleanpeg` module).

First, create textual file `csv.peg` to store the grammar.
First, create textual file `csvlang.peg` to store the grammar.

- CSV file consists of one or more records or newlines and the End-Of-File at
the end.
Expand Down Expand Up @@ -191,7 +191,7 @@ First, create textual file `csv.peg` to store the grammar.
be escaped by doubling it (`""`).


The whole grammar (i.e. the contents of `csv.peg` file) is:
The whole grammar (i.e. the contents of `csvlang.peg` file) is:

csvfile = (record / r'\n')+ EOF
record = field ("," field)*
Expand All @@ -201,27 +201,27 @@ The whole grammar (i.e. the contents of `csv.peg` file) is:
field_content_quoted = r'(("")|([^"]))+'


Now, we shall create `csv_peg.py` file in order to instantiate our parser and
Now, we shall create `csvlang_peg.py` file in order to instantiate our parser and
parse inputs. This time we shall instantiate different parser class
(`ParserPEG`). The whole content of `csv_peg.py` should be:
(`ParserPEG`). The whole content of `csvlang_peg.py` should be:

```python
from arpeggio.cleanpeg import ParserPEG

csv_grammar = open('csv.peg', 'r').read()
csv_grammar = open('csvlang.peg', 'r').read()
parser = ParserPEG(csv_grammar, 'csvfile', ws='\t ')
```

Here we load the grammar from `csv.peg` file and construct the parser using
Here we load the grammar from `csvlang.peg` file and construct the parser using
`ParserPEG` class.

The rest of the code is the same as in `csv.py`. We load `test_data.csv` and
The rest of the code is the same as in `csvlang.py`. We load `test_data.csv` and
call `parser.parse` on it to produce parse tree.

To verify that everything works without errors execute `csv_peg.py` module.
To verify that everything works without errors execute `csvlang_peg.py` module.

```bash
$ python csv_peg.py
$ python csvlang_peg.py
```

If we put the parser in debug mode and generate parse tree image we can
Expand Down
10 changes: 5 additions & 5 deletions examples/csv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ This is an example of a parser for a simple data interchange format - CSV
(Comma-Separated Values). CSV is a textual format for tabular data interchange.
It is described by RFC 4180.

`csv.py` file is an implementation using [Python grammar
`csvlang.py` file is an implementation using [Python grammar
specification](http://textx.github.io/Arpeggio/grammars/#grammars-written-in-python).
`csv_peg.py` file is the same parser implemented using [PEG grammar
`csvlang_peg.py` file is the same parser implemented using [PEG grammar
syntax](http://textx.github.io/Arpeggio/grammars/#grammars-written-in-peg-notations)
where the grammar is specified in the `csv.peg` file.
where the grammar is specified in the `csvlang.peg` file.

Run this examples:
```
$ python csv.py
$ python csv_peg.py
$ python csvlang.py
$ python csvlang_peg.py
```

The output of both examples should be the same as the same file is parsed and
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions examples/csv/csv.py → examples/csv/csvlang.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def visit_csvfile(self, node, children):


def main(debug=False):
print('Here')
# First we will make a parser - an instance of the CVS parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
Expand Down
4 changes: 2 additions & 2 deletions examples/csv/csv_peg.py → examples/csv/csvlang_peg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pprint
from arpeggio import visit_parse_tree
from arpeggio.cleanpeg import ParserPEG
from examples.csv.csv import CSVVisitor
from csvlang import CSVVisitor

def main(debug=False):
# First we will make a parser - an instance of the CVS parser model.
Expand All @@ -20,7 +20,7 @@ def main(debug=False):
# spaces. Newlines have semantics in csv files. They are used to separate
# records.
current_dir = os.path.dirname(__file__)
csv_grammar = open(os.path.join(current_dir, 'csv.peg'), 'r').read()
csv_grammar = open(os.path.join(current_dir, 'csvlang.peg'), 'r').read()
parser = ParserPEG(csv_grammar, 'csvfile', ws='\t ', debug=debug)

# Creating parse tree out of textual input
Expand Down

0 comments on commit 087c3c4

Please sign in to comment.