source: icGREP/icgrep-devel/icgrep/grep_options.py @ 5815

Last change on this file since 5815 was 5698, checked in by cameron, 17 months ago

Modularization progress

File size: 7.3 KB
Line 
1#!/usr/bin/python
2#
3# A simple script to partially automate flag processing.
4
5integer = "int"
6boolean = "bool"
7string = "std::string"
8stringlist = "std::vector<std::string>"
9
10GrepOptions = [[boolean, "i", "ignore-case", "Ignore case distinctions in the pattern and the file.", "RE_Options"],
11                  [boolean, "v", "invert-match", "Invert match results: select non-matching lines.", "RE_Options"],
12                  [boolean, "x", "line-regexp", "Require that entire lines be matched.", "RE_Options"],
13                  [boolean, "w", "word-regexp", "Require that that whole words be matched.", "RE_Options"],
14                  [stringlist, "e", "regexp", "Regular expression", "RE_Options"],
15                  [string, "f", "file", "Take regular expressions (one per line) from a file.", "RE_Options"],
16
17                  #[boolean, "F", "fixed-strings", "Interpret patterns as fixed strings, separated by newlines", "RE_Options"],
18                  #[boolean, "G", "basic-regexp", "Interpret pattern using Posix basic regular expression (BRE) syntax", "RE_Options"],
19                  #[boolean, "E", "extended-regexp", "Interpret pattern using Posix extended regular expression (ERE) syntax", "RE_Options"],
20                  #[boolean, "P", "perl-regexp", "Interpret pattern using Perl-compatible regular expression (PCRE) syntax", "RE_Options"],
21                  #[boolean, "", "PROSITE", "Interpret pattern using PROSITE protein pattern syntax", "RE_Options"],
22
23                  [boolean, "r", "recursive", "Recursively process files within directories, (but follow only top-level symlinks unless -R).", "Input_Options"],
24                  [boolean, "R", "dereference-recursive", "Recursively process files within directories, following symlinks at all levels.", "Input_Options"],
25                  [boolean, "a", "text", "Treat each input file as text, even if it is a binary file.", "Input_Options"],
26                  [boolean, "I", "binary-non-matching", "Treat binary files as non-matching.", "Input_Options"],
27                  [boolean, "U", "binary", "Treat each input file as a binary file, without CRLF normalization.", "Input_Options"],
28                  [boolean, "z", "null-data", "Use the NUL character (codepoint 00) as the line-break character for input.", "Input_Options"],
29                  [boolean, "", "mmap", "Use mmap for file input.", "Input_Options"],
30                  [string, "", "exclude", "Exclude files matching the given filename GLOB pattern.", "Input_Options"],
31                  [string, "", "exclude-from", "Exclude files matching filename GLOB patterns from the given file.", "Input_Options"],
32                  [string, "", "exclude-dir", "Exclude directories matching the given pattern.", "Input_Options"],
33                  [string, "", "include", "Include only files matching the given filename GLOB pattern.", "Input_Options"],
34
35                  #[boolean, "q", "quiet", "silent", "Do not generate any output and ignore errors; set the return to zero status if a match is found.", "Output_Options"],
36                  #[boolean, "L", "files-without-match", "Display only the names of files that do not match the pattern.", "Output_Options"],
37                  #[boolean, "l", "files-with-match", "Display only the names of files that have at least one match to the pattern.", "Output_Options"],
38                  #[boolean, "c", "count", "Display only the count of matching lines per file.", "Output_Options"],
39 
40                  [boolean, "s", "no-messages", "Suppress messages for file errors.", "Output_Options"],
41
42
43                  [boolean, "H", "with-filename", "Show the file name with each matching line.", "Output_Options"],
44                  [boolean, "h", "no-filename", "Do not show filenames with maches.", "Output_Options"],
45                  [boolean, "Z", "null", "Write NUL characters after filenames generated to output.", "Output_Options"],
46                  [boolean, "n", "line-number", "Show the line number with each matching line.", "Output_Options"],
47
48                  [boolean, "b", "byte-offset", "Show the byte offset within the file for each matching line.", "Output_Options"],
49                  [boolean, "u", "unix-byte-offsets", "If byte offsets are displayed, report offsets as if all lines are terminated with a single LF.", "Output_Options"],
50                  [boolean, "T", "initial-tab", "Line up matched line content using an inital tab character.", "Output_Options"],
51
52
53                  [boolean, "o", "only-matching", "Display only the exact strings that match the pattern, with possibly multiple matches per line.", "Output_Options"],
54
55                  [string, "", "label", "Set a label for input lines matched from stdin.", "Output_Options"],
56                  [boolean, "", "line-buffered", "Buffer lines to output.", "Output_Options"],
57                  [boolean, "", "normalize-line-breaks", "Normalize line breaks to LF.", "Output_Options"],
58
59                  # NumericOptions
60                  [integer, "A", "after-context", "Print <num> lines of context after each matching line.", "Output_Options"],
61                  [integer, "B", "before-context", "Print <num>lines of context before each matching line.", "Output_Options"],
62                  [integer, "C", "context", "Print <num> lines of context before and after each matching line.", "Output_Options"],
63                  [integer, "m", "max-count", "Process only the first <num> matches per file (overridden and set to 1 in -q, -l, -L modes).", "Output_Options"]]
64
65
66
67def CamelCase(s) :
68    capNext = True
69    t = ""
70    for c in s:
71        if c == "-": capNext = True
72        else:
73            if capNext: t += c.upper()
74            else: t += c
75            capNext = False
76    return t
77
78OptionTemplate = r"""%s %sFlag;
79static cl::opt<%s, true> %sOption("%s", cl::location(%sFlag), cl::desc("%s"), cl::cat(%s)%s);
80"""
81
82ZeroOrMoreTemplate = r"""%s %sVector;
83static cl::list<std::string, %s> %sOption("%s", cl::location(%sVector), cl::desc("%s"), cl::ZeroOrMore, cl::cat(%s)%s);
84"""
85
86AliasTemplate = r"""static cl::alias %s("%s", cl::desc("Alias for -%s"), cl::aliasopt(%sOption));
87"""
88
89ExternTemplate =r"""extern %s %s%s; // -%s
90"""
91
92UnsupportedTemplate = r"""if (%sFlag) {
93    llvm::report_fatal_error("Sorry, -%s is not yet supported.\n");
94}
95"""
96
97if __name__ == "__main__":
98    p = ""
99    e = ""
100    u= ""
101    for option_spec in GrepOptions:
102        datatype = option_spec[0]
103        flag = option_spec[1]
104        desc = option_spec[-2]
105        cat = option_spec[-1]
106        aliases = option_spec[2:-2]
107        name = CamelCase(aliases[0])
108        grouping = ", cl::Grouping"
109        if flag == "":
110            flag = aliases[0]
111            aliases = aliases[1:]
112            grouping = ""
113        if datatype == stringlist:
114            basetype = string
115            p += ZeroOrMoreTemplate % (datatype, name, datatype, name, flag, name, desc, cat, grouping) 
116        else:
117            p += OptionTemplate % (datatype, name, datatype, name, flag, name, desc, cat, grouping) 
118        for a in aliases:
119            p+= AliasTemplate % (CamelCase(a) + "Alias", a, flag, name)
120        p += "\n"
121        if datatype == stringlist:
122            e += ExternTemplate % (datatype, name, "Vector", flag)
123        else:
124            e += ExternTemplate % (datatype, name, "Flag", flag)
125        u += UnsupportedTemplate % (name, flag)
126    print p
127    print e
128    print u
129
130
131
132
133
134
135
136
137
138
139
140
Note: See TracBrowser for help on using the repository browser.