packcc

util/tab.peg (version 1.0.0)

Synopsis

An import file that provides the following rule:

Usage

The usage procedure is shown below.

  1. Import util/tab.peg after the last rule in the PEG file.
    %import "util/tab.peg"
    
  2. Set the tab size if necessary as follows:
    %source {
    #define PCC_TAB_SIZE 4
    }
    

    In the example above, the tab size is set to 4. Without the definition, the default tab size 8 is applied.

    If the prefix is set to %prefix, the macro stating with PCC\_ is changed to that with the specified prefix as below.

    %prefix "my"
    
    %source {
    #define MY_TAB_SIZE 4
    }
    
  3. Use the rule TAB provided by this import file to match a space character ' ' or a tab character '\t'.
    rule <- TAB* '\n'
    

    In the example above, the rule matches any length including 0 of a sequence containing space and tab characters followed by '\n'.

  4. Use the marker variable @tab_col to measure the column number.
    rule <- &{ @tab_col = 0; } TAB* { printf("column: %d\n", (int)@tab_col); } '\n'
    

    In the example above, @tab_col is set to 0 in advance, and @tab_col will have the column number at the position after the part matched by TAB*.

Example

An example which shows column numbers of the respective input sequences of space and tab characters followed by '\n' is shown here.

%prefix "my"

%source {
#define MY_TAB_SIZE 4
}

rule <- &{ @tab_col = 0; } TAB* { printf("column: %d\n", (int)@tab_col); } '\n'

%import "util/tab.peg"

%%
int main(int argc, char **argv) {
    my_context_t *ctx = my_create(NULL);
    while (my_parse(ctx, NULL));
    my_destroy(ctx);
    return 0;
}

Some output results of the generated parser are shown below.