Skip to content

Instantly share code, notes, and snippets.

@nilium
Created March 21, 2009 07:56
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 nilium/82766 to your computer and use it in GitHub Desktop.
Save nilium/82766 to your computer and use it in GitHub Desktop.
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