forked from jamiejennings/rosie-pattern-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.rpl
89 lines (69 loc) · 3.78 KB
/
common.rpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---- -*- Mode: rpl; -*-
----
---- common.rpl Common patterns in Rosie Pattern Language
----
---- © Copyright IBM Corporation 2016.
---- LICENSE: MIT License (https://opensource.org/licenses/mit-license.html)
---- AUTHOR: Jamie A. Jennings
alias any = .
alias whitespace = [:space:]
alias digit = [:digit:]
alias letter = [:alpha:]
-- [:graph:] won't match unicode, but these will:
alias common.graph = {![:space:] .}
alias common.id_char = [_-] / {![:space:] ![:punct:] .}
alias hex_only = { [a-f] / [A-F] }
alias hex_digits = { digit / [a-f] / [A-F] }
--alias common.hex = { digit hex_digits+ } / { hex_only+ digit hex_digits* }
common.int = { [+-]? digit+ !hex_only} -- at least one digit, and not a hex number
common.float = { [+-]? digit+ "." digit+ } -- float with digits on either side of radix
common.hex = hex_digits+ -- use with care! will match words and decimal numbers
common.denoted_hex = { "0x" common.hex }
common.number = (common.denoted_hex / common.float / common.int / common.hex)
common.word = letter+
-- This definition is essentially what grok uses, which isn't great:
-- common.unix_path = { "/" ([:alnum:]/[_%!$@:.,~-])+ / ".." / "." }+
alias common.unix_path = { {".." / "."}? {"/" {[:alnum:]/[_%!$@:.,~-]}+ }+ }
--alias common.pathchar = [[:alnum:][_%!$@:.,~-]]
--alias common.unix_path = { {"../" / "./" / "/"}? {pathchar+ {"/" pathchar+}+ } }
alias common.windows_path = { {[:alpha:]+ ":"}? {"\\" {![\\?*] any}* }+ }
common.path = common.unix_path / common.windows_path
alias dash_under = [-_]
alias identifier_char = { letter / digit / "_" }
alias identifier_plus_char = { letter / digit / dash_under / "$" }
common.identifier = {letter identifier_char*}
common.identifier_plus = {letter identifier_plus_char*}
alias identifier_plus_plus1 = { { {letter / dash_under / "$"} identifier_plus_char*} }
alias identifier_plus_plus2 = {digit+ identifier_plus_char*} -- FIXME: I match integers!
common.identifier_plus_plus = identifier_plus_plus1 / identifier_plus_plus2
common.dotted_identifier = { common.identifier_plus_plus { "." common.identifier_plus_plus}+ }
-- starts with a letter and contains a digit, or starts with a letter
--and contains a dash/under (otherwise, all the identifier_plus
--characters are allowed)
common.identifier_not_word = { {letter {letter / dash_under}* [:digit:] identifier_plus_char*}
/ {letter {letter / digit}* dash_under identifier_plus_char*} }
-- Upper case words which may also contain digits, dashes,
-- underlines. Note that we explicitly fail if the first upper case
-- letter is followed by a lower case one, since most sentences and
-- proper names start that way.
common.maybe_identifier = { [:upper:] ![:lower:] {[:upper:] / [:digit:] / dash_under}* }
alias h = [:xdigit:]
alias common.type4_guid_nocap = {h{8,8} "-" {h{4,4} "-"}{3,3} h{12,12}}
common.type4_guid = common.type4_guid_nocap
-- Quoted strings, using single or double quote
--
-- Within a string, the quote character can be escaped using a slash, OR by repeating it twice,
-- e.g. "".
alias common.dquoted_string = "\"" {"\\\"" / {! [\"] .}}* "\""
alias dquote = "\"" -- "
alias escaped_dquote = "\\\"" / "\\\"\\\"" / "\"\"" -- \" or \"\" or ""
alias squote = "'" -- '
alias escaped_squote = "\\'" / "\\'\\'" / "''" -- \' or \'\' or ''
-- Capture the contents of a quoted field by assigning it to an RPL name:
double_quoted_field = {escaped_dquote / {!dquote .}}*
single_quoted_field = {escaped_squote / {!squote .}}*
-- Wrap the field contents with quotation marks:
alias common.dq = {dquote double_quoted_field dquote}
alias common.sq = {squote single_quoted_field squote}
-- Look for either double or single quoted fields
common.quoted_field = common.dq / common.sq