Skip to content

Instantly share code, notes, and snippets.

@peta
Last active March 21, 2020 04:58
Show Gist options
  • Save peta/5174284 to your computer and use it in GitHub Desktop.
Save peta/5174284 to your computer and use it in GitHub Desktop.
Regex design pattern for Textmate language grammars. An absolutely cool feature of PCRE (even supported by Oniguruma) are named subexpression calls. Allowing for recursive matching while preserving (atomic) contexts at every recursion level in form of named match results. However, there is no light without shadow: When no multiline matches can b…
myPattern = {
name = 'meta.spec.triples';
begin = '(?x)
(?<subject> \g<iri> | \g<BlankNode> | \g<collection> ){0}
(?<iri> \g<IRIREF> | \g<PrefixedName> ){0}
(?<IRIREF> \< (?:[^\x00-\x20\<\>\\\"\{\}\|\^`] | (?:\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8}))* \> ){0}
(?<PrefixedName> \g<PNAME_LN> | \g<PNAME_NS> ){0}
(?<PNAME_LN> (\g<PNAME_NS>) ( (?: [\p{L}\p{M}_] | [:0-9] | %[0-9A-Fa-f]{2} | [\\_~.\-!$&''\(\)*+,;=/?#@%] ) (?:(?:[\p{L}\p{M}_\-0-9\u00B7\u0300-\u036F\u203F-\u2040] | [:.] | %[0-9A-Fa-f]{2} | [\\_~.\-!$&''\(\)*+,;=/?#@%])* (?:[\p{L}\p{M}_\-0-9\u00B7\u0300-\u036F\u203F-\u2040] | : | %[0-9A-Fa-f]{2} | [\\_~.\-!$&''\(\)*+,;=/?#@%])) )? ){0}
(?<PNAME_NS> (?: [\p{L}\p{M}] (?:(?:[\p{L}\p{M}_.\-0-9\u00B7\u0300-\u036F\u203F-\u2040])* [\p{L}\p{M}_\-0-9\u00B7\u0300-\u036F\u203F-\u2040] )? )? \: ){0}
(?<BlankNode> \g<BLANK_NODE_LABEL> | \g<ANON> ){0}
(?<BLANK_NODE_LABEL> _: ([\p{L}\p{M}_0-9]) ([\p{L}\p{M}_\-0-9\u00B7\u0300-\u036F\u203F-\u2040.]* [\p{L}\p{M}_\-0-9\u00B7\u0300-\u036F\u203F-\u2040])? ){0}
(?<ANON> \[[\x20\x9\xD\xA]*\] ){0}
(?<collection> \(\g<object>*\) ){0}
(?<object> \g<iri> | \g<BlankNode> | \g<collection> | \g<blankNodePropertyList> | \g<literal> ){0}
(?<blankNodePropertyList> \[\g<predicateObjectList>\] ){0}
(?<predicateObjectList> \g<verb>\s+\g<objectList> (?:[ ]*;\s+\g<verb>\s+\g<objectList> )* ){0}
(?<verb>\s\g<predicate>\s|\sa\s){0}
(?<predicate> \g<iri> ){0}
(?<objectList> \g<object> (?:,\s\g<object>)* ){0}
(?<literal> "([^\x22\x5C\xA\xD] | \\[tbnrf"''] | (?:\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8}))*" ){0}
(?#Here starts the show)
^(?i)(?!@|\#|PREFIX|BASE)(?-i)(?:[ ]*)(\g<subject>)\b
';
end = '(?x)(?#Here starts the show)';
captures = {
ANON = { name = 'meta.tmp.ANON'; };
BlankNode = { name = 'meta.tmp.BlankNode'; };
IRIREF = { name = 'meta.tmp.IRIREF'; };
PNAME_LN = { name = 'meta.tmp.PNAME_LN'; };
PNAME_NS = { name = 'meta.tmp.PNAME_NS'; };
PrefixedName = { name = 'meta.tmp.PrefixedName'; };
blankNodePropertyList = { name = 'meta.tmp.blankNodePropertyList'; };
collection = { name = 'meta.tmp.collection'; };
iri = { name = 'meta.tmp.iri'; };
literal = { name = 'meta.tmp.literal'; };
object = { name = 'meta.tmp.object'; };
objectList = { name = 'meta.tmp.objectList'; };
predicate = { name = 'meta.tmp.predicate'; };
predicateObjectList = { name = 'meta.tmp.predicateObjectList'; };
subject = { name = 'meta.tmp.subject'; };
verb = { name = 'meta.tmp.verb'; };
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment