Learning regular expressions-wtl80 appwizmobile's splitcode Function

Source: Internet
Author: User
In wtl80, the difference between the appwiz Application Wizard and the appwizmobile Application Wizard is that the splitcode function is used in appwizmobile to provide only the wtl wizard template. h file, you can use splitcode to convert class member functions. in the CPP file, while the appwiz Application Wizard is compiled. H and. CPP file, according [! If wtl_use_cpp_files]/[! Endif] for preprocessing. In contrast, the use of splitcode is much easier to compile the wizard template.

In the current version of visualfc (0.80116601), the built-in Appwizard and CodeWizard use the same processing method as the appwiz of wtl80, that is [! If wtl_use_cpp_files]/[! Endif. To simplify the preparation of the visualfc wizard template, we will consider imitating the appwizmobile Wizard of wtl80 in later versions of visualfc to provide the splitcode function for processing. The splitcode function of the wtl80 wizard is completed using JScript. here we can use boost: RegEx to rewrite it for direct use in C ++ programs. The regular expressions used for analysis of C ++ classes and member functions are completely consistent in the two languages. The specific code is as follows:

Splitcode code /********************************* **************************************** * ***** description: add the function members body to. CPP file and remove them from the matching header. do nothing if no matching. h file. cpppath: path name to. CPP file ************************************** **************************************** /function splitcode (cpppath) {var FSO = new acti Vexobject ('scripting. FileSystemObject '); var hpath = cpppath. Replace (/. cpp $/, ". H"); If (! FSO. fileexists (hpath) return; var cppfile = FSO. getFile (cpppath); var hfile = FSO. getFile (hpath); var TSH = hfile. openastextstream (1); var htext = Tsh. readall (); Tsh. close (); var tscpp = cppfile. openastextstream (8); var classpattern =/^ [/T] * class [/T] + (/W +) [/S] +? (?: ^ [/T] */}; $)/mgvar classinfo = htext. match (classpattern); var numclass = classinfo. length; For (NC = 0; NC <numclass; NC ++) {var classtext = classinfo [Nc]; classtext. match (classpattern); var classname = Regexp. $1; var fnpattern =/(^ (?: [/T] +/W +) * [/T] + (/W +)/([^/)] */) ([/S] */{([^/}] */{[^/}] */}) * [^/}] */} + ?) /Mgvar fnpatternif =/(^ (?: # If. +/S + )*(?: [/T] +/W +) * [/T] + (/W +)/([^/)] */) ([/S] */{([^/}] */{[^/}] */}) * [^/}] */} +? (? :/S + # E (?: Ndif | LIF | LSE) [^/R/n] *)/mgvar fninfo = classtext. match (fnpatternif); var numfn = fninfo. length; For (n = 0; n <numfn; n ++) {var fntextif = fninfo [N]; var fntextif = fntextif. match (fnpatternif); var fndef = Regexp. $1; var fnname = Regexp. $2; var fnbody = Regexp. $3; var fnfullname = classname + ":" + fnname; fndef = fndef. replace (fnname, fnfullname); fndef = fndef. replace (/^/t (? :/S * virtual/S +) */, ""); fnbody = fnbody. replace (/^/T/mg, ""); tscpp. write (fndef); tscpp. write (fnbody); tscpp. writeblanklines (2); var fntext = fntextif. input. match (fnpattern); var fndecl = Regexp. $1 + ";"; classtext = classtext. replace (fntext, fndecl);} htext = htext. replace (classinfo [Nc], classtext);} tscpp. close (); TSH = hfile. openastextstream (2); Tsh. write (htext); Tsh. close ();} Use boost: RegEx to rewrite the Code. The Code is slightly different and is not implemented. File Processing, directly processing strings. Void process (const STD: string & text) {STD: stringregstr = "^ [/T] * class [/T] + (// W +) [// S // s] +? (?: ^ [/T] * //}; $) "; Boost: RegEx expression (regstr, boost: regbase: javascript); STD: String :: const_iterator start, end; Start = text. begin (); End = text. end (); Boost: match_results <STD: String: const_iterator> what; Boost: match_flag_type flags = boost: match_default; STD: String file_h = text; STD:: String file_cpp; while (boost: regex_search (START, end, what, expression, flags) {start = what [0]. second; STD: St Ring class_name = what [1]; STD: String class_text = what [0]; STD: String class_h, class_cpp; process_class (class_name, class_text, class_h, class_cpp ); file_h = boost: regex_replace (file_h, boost: RegEx (class_text, boost: regbase: literal), class_h); file_cpp + = class_cpp ;}} void process_class (const STD: string & class_name, const STD: string & class_text, STD: string & class_h, STD: string & class_cpp) {STD :: String fnpattern = "(^ (?: [/T] + // W +) * [/T] + (// W +) // ([^/)] * //) ([// s] * // {([^ //}] * // {[^ //}] * //}) * [^ //}] * //} + ?) "; STD: String fnpatternif =" (^ (?: # If. + // s + )*(?: [/T] + // W +) * [/T] + (// W +) // ([^/)] * //) ([// s] * // {([^ //}] * // {[^ //}] * //}) * [^ //}] * //} +? (?: // S + # E (?: Ndif | LIF | LSE) [^/R/n] *) "; STD: String: const_iterator start, end; Start = class_text.begin (); end = class_text.end (); Boost: match_results <STD: String: const_iterator> what; Boost: match_flag_type flags = boost: match_default; Boost: RegEx expression (fnpatternif, boost: regbase: javascript); class_h = class_text; class_cpp = "/R/N // class/t"; class_cpp + = class_name; class_cpp + = _ T ("/R/N"); While (boost: Rege X_search (START, end, what, expression, flags) {start = what [0]. second; STD: String func_buffer = what [0]; STD: String func_def = what [1]; STD: String func_name = what [2]; STD :: string func_body = what [3]; // write CPP functionstd: String func_fullname = STD: string (class_name) + ":" + func_name; func_def = boost :: regex_replace (func_def, boost: RegEx (func_name), func_fullname); func_def = boost: regex_repla Ce (func_def, boost: RegEx ("^/t (?: // S * virtual/S +) * ")," "); func_body = boost: regex_replace (func_body, boost: RegEx (" ^/t "), ""); class_cpp + = _ T ("/R/N"); class_cpp + = func_def; class_cpp + = func_body; class_cpp + = _ T ("/R/n/R/N"); Boost: regex_search (func_buffer, what, boost: RegEx (fnpattern), flags ); // STD: String func_decl = (STD: string) (what [1]) + ";"; Boost: RegEx Reg (STD: string (what. base (), boost: regbase: literal); class_h = boost: regex_replace (class_h, Reg, func_decl );}}

Through comparison, we can also see that the code is more complex than JScript because C ++ does not have built-in RegEx processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.