Created
March 21, 2009 07:56
-
-
Save nilium/82766 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFIER_REGEX=/[a-zA-Z_]\w*/ | |
TYPENAME_REGEX=/(?ix) | |
# type shortcut | |
(?: | |
( [!#%] | @{1,2} | \$[zw]? ) | |
| | |
: \s* ([a-zA-Z_]\w*) | |
) | |
# pointer | |
(?: \s+ (Ptr) \b)* | |
# array | |
(\[[^\]]+\])? | |
# reference | |
(?: \s+ (Var) \b)? | |
/ | |
# value (name[type][ = value]) | |
VALUE_REGEX=/(?ix) | |
(?<name>#{IDENTIFIER_REGEX}) | |
\s* (?<typename>#{TYPENAME_REGEX}(?:\(.*?\))?) | |
(?:\s* = \s*(?<value>.+))? | |
/ | |
FIELD_REGEX=/(?ix)^field\s+#{VALUE_REGEX}$/ | |
foo = "field foobar:Object Ptr Ptr = Null" | |
puts FIELD_REGEX.inspect | |
if md = FIELD_REGEX.match(foo) then | |
puts md[:name] | |
puts md[:typename] | |
puts md[:value] | |
puts md.inspect | |
end | |
# yay for regular expressions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment