Skip to content

Instantly share code, notes, and snippets.

@bryan-ash
Created January 6, 2010 01:25
Show Gist options
  • Save bryan-ash/269918 to your computer and use it in GitHub Desktop.
Save bryan-ash/269918 to your computer and use it in GitHub Desktop.
Treetop grammar to find array of C enum elements
grammar Spike
rule typedef
'typedef enum { ' typedef_items ' }'
{
def items
typedef_items.items
end
}
end
rule typedef_items
first_item:item other:(',' s item)*
{
def items
[first_item.name] + other_item_names
end
def other_item_names
other.elements.map do |comma_and_item|
comma_and_item.item.name
end
end
}
end
rule item
[\w]+
{
def name
text_value
end
}
end
rule s # Optional space
S?
end
rule S # Mandatory space
(white / comment_to_eol / comment_c_style)+
end
rule white
[ \t\n\r]+
end
rule comment_to_eol
'//' (!"\n" .)+
end
rule comment_c_style
'/*' (!'*/' . )* '*/'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment