1 %option c++ debug stack noyywrap yylineno
2 
3 %{
4 
5 #include <cstdlib>
6 #include <iostream>
7 #include <string>
8 #include <vector>
9 
10 #include "Converter.hpp"
11 #include "BashConverter.hpp"
12 #include "ZshConverter.hpp"
13 
14 static std::string buffer,
15     command,
16     description;
17 static std::vector<std::string> short_option,
18     long_option,
19     old_option;
20 static std::vector<Converter*> converters;
21 
log(std::string s)22 void log(std::string s) {
23     //std::cout << s << std::endl;
24 }
25 
26 %}
27 
28 SPACE [ \t]+
29 
30 %x X_line X_c X_d X_o X_s X_l X_s_quoted X_d_quoted
31 
32 %%
33 
34 complete {
35     yy_push_state(X_line);
36     log("begin line");
37 }
38 
39 <X_line>(-c|--command){SPACE}      yy_push_state(X_c);
40 <X_line>(-d|--description){SPACE}  yy_push_state(X_d);
41 <X_line>(-o|--old-option){SPACE}   yy_push_state(X_o);
42 <X_line>(-s|--short-option){SPACE} yy_push_state(X_s);
43 <X_line>(-l|--long-option){SPACE}  yy_push_state(X_l);
44 
45 <X_c,X_d,X_o,X_s,X_l>"\'" {
46     buffer += yytext;
47     yy_push_state(X_s_quoted);
48 }
49 <X_s_quoted>"\\\'" buffer += "\'\"\'\"\'";
50 <X_s_quoted>"\'"   {
51     buffer += yytext;
52     yy_pop_state();
53 }
54 
55 <X_c,X_d,X_o,X_s,X_l>"\"" {
56     buffer += yytext;
57     yy_push_state(X_d_quoted);
58 }
59 <X_d_quoted>"\\\"" buffer += "\"\'\"\'\"";
60 <X_d_quoted>"\""   {
61     buffer += yytext;
62     yy_pop_state();
63 }
64 
65 <X_s_quoted,X_d_quoted>"\["    buffer += "\\\\[";
66 <X_s_quoted,X_d_quoted>"\]"    buffer += "\\\\]";
67 <X_s_quoted,X_d_quoted>"\`"    buffer += "\'\"\'\"\'";
68 <X_s_quoted,X_d_quoted>{SPACE} buffer += " ";
69 <X_s_quoted,X_d_quoted>.       buffer += yytext;
70 
71 <X_c>{SPACE}|\n {
72     command = buffer;
73     buffer = "";
74     yy_pop_state();
75     if (strcmp(yytext, "\n") == 0) {
76         yyless(0);
77     }
78     log("command: " + command);
79 }
80 <X_d>{SPACE}|\n {
81     description = buffer;
82     buffer = "";
83     yy_pop_state();
84     if (strcmp(yytext, "\n") == 0) {
85         yyless(0);
86     }
87     log("description: " + description);
88 }
89 <X_o>{SPACE}|\n {
90     old_option.push_back(buffer);
91     buffer = "";
92     yy_pop_state();
93     if (strcmp(yytext, "\n") == 0) {
94         yyless(0);
95     }
96     log("old_option: " + old_option.back());
97 }
98 <X_s>{SPACE}|\n {
99     short_option.push_back(buffer);
100     buffer = "";
101     yy_pop_state();
102     if (strcmp(yytext, "\n") == 0) {
103         yyless(0);
104     }
105     log("short_option: " + short_option.back());
106 }
107 <X_l>{SPACE}|\n {
108     long_option.push_back(buffer);
109     buffer = "";
110     yy_pop_state();
111     if (strcmp(yytext, "\n") == 0) {
112         yyless(0);
113     }
114     log("long_option: " + long_option.back());
115 }
116 <X_c,X_d,X_o,X_s,X_l>. buffer += yytext;
117 
118 <X_line>\n {
119     yy_pop_state();
120 
121     for (auto &it : converters) {
122         it->convert(
123             command,
124             description,
125             short_option,
126             long_option,
127             old_option);
128     }
129 
130     buffer =
131         command =
132         description =
133         "";
134     short_option.clear();
135     long_option.clear();
136     old_option.clear();
137 
138     log("end line");
139 }
140 
141 .|\n {}
142 
143 %%
144 
145 int main() {
146     BashConverter bash_converter;
147     converters.push_back((Converter*)&bash_converter);
148 
149     ZshConverter zsh_converter;
150     converters.push_back((Converter*)&zsh_converter);
151 
152     FlexLexer* lexer = new yyFlexLexer;
153     //lexer->set_debug(1);
154     while(lexer->yylex() != 0)
155         ;
156 
157     return 0;
158 }
159