Skip to content

Instantly share code, notes, and snippets.

@rns
Created September 9, 2014 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rns/3b2f48477fc23d0ab0f7 to your computer and use it in GitHub Desktop.
Save rns/3b2f48477fc23d0ab0f7 to your computer and use it in GitHub Desktop.
use 5.010;
use strict;
use warnings;
use Marpa::R2 2.090; # for parse()
my $g = Marpa::R2::Scanless::G->new( {
source => \(<<'END_OF_SOURCE'),
:default ::= action => [ name, value]
lexeme default = action => [ name, value] latm => 1
file ::= directive+
directive ::= define | if | include
define ::= '#define' const value
if ::= '#if' '(' const ')' defines else defines endif
else ::= '#else' comment
else ::= '#else'
endif ::= '#endif' comment
endif ::= '#endif'
include ::= '#include' '"' file_name '"'
defines ::= define+
file_name ~ [a-zA-Z_\.0-9]+
const ~ [A-Z_0-9]+
value ~ non_nl [\n]
non_nl ~ [^\n]+
comment ~ '//' non_nl [\n]
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my $input = <<EOI;
#include "File_A1.h"
#include "File_A2.h"
#if (FAST_FLUSH_SUPPORT )
#define MEMORY_BUDGET_FLASH_DIR (12 * 256)
#define MEMORY_BUDGET_FLASH_CFG (15 * 256)
#else //(FAST_FLUSH_SUPPORT )
#define MEMORY_BUDGET_FLASH_DIR (0)
#define MEMORY_BUDGET_FLASH_CFG (0)
#endif //(FAST_FLUSH_SUPPORT )
#define DRAM_OFFSET_FLASH_DIR (DUMMY_ENGR2_LOAD_ADDRESS)
#define DRAM_OFFSET_FLASH_CFG (DRAM_OFFSET_FLASH_DIR + MEMORY_BUDGET_FLASH_DIR)
#define DRAM_OFFSET_FLASH_LOG (DRAM_OFFSET_FLASH_CFG + MEMORY_BUDGET_FLASH_CFG)
EOI
use YAML;
say Dump ${ $g->parse( {
input => \$input,
} ) };
@rns
Copy link
Author

rns commented Sep 9, 2014


---
- file
-
  - directive
  -
    - include
    - '#include'
    - '"'
    -
      - file_name
      - File_A1.h
    - '"'
-
  - directive
  -
    - include
    - '#include'
    - '"'
    -
      - file_name
      - File_A2.h
    - '"'
-
  - directive
  -
    - if
    - '#if'
    - (
    -
      - const
      - FAST_FLUSH_SUPPORT
    - )
    -
      - defines
      -
        - define
        - '#define'
        -
          - const
          - MEMORY_BUDGET_FLASH_DIR
        -
          - value
          - "                   (12 * 256)\n"
      -
        - define
        - '#define'
        -
          - const
          - MEMORY_BUDGET_FLASH_CFG
        -
          - value
          - "                   (15 * 256)\n"
    -
      - else
      - '#else'
      -
        - comment
        - "//(FAST_FLUSH_SUPPORT )\n"
    -
      - defines
      -
        - define
        - '#define'
        -
          - const
          - MEMORY_BUDGET_FLASH_DIR
        -
          - value
          - "                   (0)\n"
      -
        - define
        - '#define'
        -
          - const
          - MEMORY_BUDGET_FLASH_CFG
        -
          - value
          - "                   (0)\n"
    -
      - endif
      - '#endif'
      -
        - comment
        - "//(FAST_FLUSH_SUPPORT )\n"
-
  - directive
  -
    - define
    - '#define'
    -
      - const
      - DRAM_OFFSET_FLASH_DIR
    -
      - value
      - "                   (DUMMY_ENGR2_LOAD_ADDRESS)\n"
-
  - directive
  -
    - define
    - '#define'
    -
      - const
      - DRAM_OFFSET_FLASH_CFG
    -
      - value
      - "                    (DRAM_OFFSET_FLASH_DIR            + MEMORY_BUDGET_FLASH_DIR)\n"
-
  - directive
  -
    - define
    - '#define'
    -
      - const
      - DRAM_OFFSET_FLASH_LOG
    -
      - value
      - "                   (DRAM_OFFSET_FLASH_CFG             + MEMORY_BUDGET_FLASH_CFG)\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment