Crates.io | indent_tokenizer |
lib.rs | indent_tokenizer |
version | 0.4.0 |
source | src |
created_at | 2017-04-17 17:31:55.030384 |
updated_at | 2018-01-29 19:55:29.539667 |
description | Generate tokens based on indentation |
homepage | |
repository | https://github.com/jleahred/indent_tokenizer |
max_upload_size | |
id | 11012 |
size | 71,422 |
= Identation tokenizer
A small an simple indentation tokenizer
A similar project is https://github.com/jleahred/indentation_flattener[indentation_flattener]. In this case we flatten the input adding PUSH_INDENT, and POP_INDENT. This looks better for PEG grammars.
== Usage
cargo.toml
[source, toml]See example below
== Modifs
0.2.0:: Removed general types (as String, u32 or usize) + Using concrete types (new types)
== Indentation format
Tabs are no valid on indentation grouping.
Let's see by example.
Indentation groups can have any number of spaces
It's not a good idea to have same level with different spaces, but it's allowed when you are creating a new level.
In this example, last level1 is idented with more spaces than previus ones
In order to go back a level, the indentation has to match with the previous on this level.
As we saw in previous example, increasing level is free indentation.
You can start lines with |
, but it's optional.
Look that |
is one position previous to indentation level.
It is usefull when you need to start with spaces.
|
|
|...... This one starts with .
A line is empty when there are no content or it only has spaces.
..... ..... ..... ..... ..... next line is empty
..... next line is empty
..... ..... next line is empty
What if I want represent empty lines?
..... ..... ..... ..... ..... I want new line after this line |
..... and three new lines, please
| | |
What if I want to represent spaces at end of line?
Spaces at end of line will not be erased, therefore, you don't need to do anything about it.
But could be intesting to represent it because some editors can run trailing or just because you can visualize it.
..... ..... ..... ..... This line keeps 2 spaces and end | and you know it
Next line is properly indented and only has spaces
| |
In fact, you can write |
at end of all lines. It will be removed.
Next strings, are equivalent.
|
it's optional at end of line.....| .....| .....| .....|
..... ..... ..... .....
But I could need a pipe |
at end of line
..... ..... ..... ..... This line ends with a pipe||
|..... ..... <- Invalid, remember, indentation mark | is previus to real indentation
|..... ..... <- This is OK, but not elegant
| .... <- I want to start with an space |..... <- This is redundant, but more clear
== Tokens
Each change of leven represent an end of token.
An empty line, is used to separate tokens on same level
A token contain lines and a list of tokens
This is the first token This is another token, because it's on a different level And another token This is also a different token
A token can contain multiple lines This is another token with three lines
Empty lines can be used to separate tokens This is a token, that continues here. Next empty line define a token division
And this is a different one
with a couple of lines
== Identation tokenizer API
Version 0.2 removed general types as String, usize, u32...
Instead, it's created an specific type on each context.
#[derive(Debug, PartialEq, Copy, Clone)] pub struct LineNum(u32);
Internally, the system uses more new types as NSpaces to represent number of spaces
Thats all
Look into lib.rs to see the api and tests.rs to se examples
== Examples
You can look into tests.rs, there are several tests.
let tokens = tokenize("
0 || 01a 01b 01c
02a
02b
|020a
||020b
| 021a
|021b
1a 1b 11a ||11b 11c
12a ||
|12b || 2a 21a 21b | |
The result will be