Skip to content

Commit

Permalink
normalize syntax define
Browse files Browse the repository at this point in the history
  • Loading branch information
vipally committed Mar 18, 2021
1 parent bae21b0 commit ca0f6cb
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 98 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Site : [https://github.com/vipally](https://github.com/vipally)
#GOGP_GPGCFG(<config-name>)
```
- "**replace**" syntax
```
```go
****<src> -> <dst>, literal replacement****
// #GOGP_REPLACE(<src>, <dst>)
```
Expand All @@ -193,21 +193,21 @@ Site : [https://github.com/vipally](https://github.com/vipally)
// #GOGP_GPONLY_END
```
- "**file-begin**" syntax
```
```go
// #GOGP_FILE_BEGIN
```
- "**file-end**" syntax
```
```go
// #GOGP_FILE_END
```
- "**once**" syntax
```
```go
// #GOGP_ONCE
{only generate once from a gp file}
// #GOGP_END_ONCE
```
- "**comment**" syntax
```
```go
//#GOGP_COMMENT {comment(in line) content}
```

Expand Down
72 changes: 72 additions & 0 deletions gpg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,75 @@ tail
fmt.Println("rep", rep)
rep = rep
}

/*
// // to remove code
// // // #GOGP_COMMENT
// expTxtGogpComment = `(?sm:(?P<COMMENT>/{2,}[ |\t]*#GOGP_COMMENT))`
// //generic-programming flag <XXX>
// expTxtTodoReplace = `(?P<P>.?)(?P<W>\<[[:alpha:]_][[:word:]]*\>)(?P<S>.?)`
// // ignore all text format:
// // //#GOGP_IGNORE_BEGIN <content> //#GOGP_IGNORE_END
// expTxtIgnore = `(?sm:\s*//#GOGP_IGNORE_BEGIN(?P<IGNORE>.*?)(?://)??#GOGP_IGNORE_END.*?$[\r|\n]*)`
// expTxtGPOnly = `(?sm:\s*//#GOGP_GPONLY_BEGIN(?P<GPONLY>.*?)(?://)??#GOGP_GPONLY_END.*?$[\r|\n]*)`

// // select by condition <cd> defines in gpg file:
// // //#GOGP_IFDEF <cd> <true_content> //#GOGP_ELSE <false_content> //#GOGP_ENDIF
// // "<key> || ! <key> || <key> == xxx || <key> != xxx"
// expTxtIf = `(?sm:^(?:[ |\t]*/{2,}[ |\t]*)#GOGP_IFDEF[ |\t]+(?P<CONDK>[[:word:]<>\|!= \t]+)(?:.*?$[\r|\n]?)(?P<T>.*?)(?:(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ELSE(?:.*?$[\r|\n]?)[\r|\n]*(?P<F>.*?))?(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ENDIF.*?$[\r|\n]?)`
// expTxtIf2 = `(?sm:^(?:[ |\t]*/{2,}[ |\t]*)#GOGP_IFDEF2[ |\t]+(?P<CONDK2>[[:word:]<>\|!= \t]+)(?:.*?$[\r|\n]?)(?P<T2>.*?)(?:(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ELSE2(?:.*?$[\r|\n]?)[\r|\n]*(?P<F2>.*?))?(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ENDIF2.*?$[\r|\n]?)`

// // " <key> || !<key> || <key> == xxx || <key> != xxx "
// // [<NOT>] <KEY> [<OP><VALUE>]
// expCondition = `(?sm:^[ |\t]*(?P<NOT>!)?[ |\t]*(?P<KEY>[[:word:]<>]+)[ |\t]*(?:(?P<OP>==|!=)[ |\t]*(?P<VALUE>[[:word:]]+))?[ |\t]*)`

// //#GOGP_SWITCH [<SWITCHKEY>] <CASES> #GOGP_GOGP_ENDSWITCH
// expTxtSwitch = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)(?:#GOGP_SWITCH)(?:[ |\t]+(?P<SWITCHKEY>[[:word:]<>]+))?(?:[ |\t]*?.*?$)[\r|\n]*(?P<CASES>.*?)(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_ENDSWITCH.*?$[\r|\n]?)`

// //#GOGP_CASE <COND> <CASE> #GOGP_ENDCASE
// //#GOGP_DEFAULT <CASE> #GOGP_ENDCASE
// expTxtCase = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)(?:(?:#GOGP_CASE[ |\t]+(?P<COND>[[:word:]<>\|!]+))|(?:#GOGP_DEFAULT))(?:[ |\t]*?.*?$)[\r|\n]*(?P<CASE>.*?)(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_ENDCASE.*?$[\r|\n]*)`

// // require another gp file:
// // //#GOGP_REQUIRE(<gpPath> [, <gpgSection>])
// expTxtRequire = `(?sm:\s*(?P<REQ>^[ |\t]*(?://)?#GOGP_REQUIRE\((?P<REQP>[^\n\r,]*?)(?:[ |\t]*?,[ |\t]*?(?:(?P<REQN>[[:word:]|#|@]*)|#GOGP_GPGCFG\((?P<REQGPG>[[:word:]]+)\)))??(?:[ |\t]*?\))).*?$[\r|\n]*(?:(?://#GOGP_IGNORE_BEGIN )?///require begin from\([^\n\r,]*?\)(?P<REQCONTENT>.*?)(?://)?(?:#GOGP_IGNORE_END )?///require end from\([^\n\r,]*?\))?[\r|\n]*)`
// expTxtEmptyLine = `(?sm:(?P<EMPTY_LINE>[\r|\n]{3,}))`

// //must be "-sm", otherwise it with will repeat every line
// expTxtTrimEmptyLine = `(?-sm:^[\r|\n]*(?P<CONTENT>.*?)[\r|\n]*$)`

// // get gpg config string:
// // #GOGP_GPGCFG(<cfgName>)
// expTxtGetGpgCfg = `(?sm:(?://)?#GOGP_GPGCFG\((?P<GPGCFG>[[:word:]]+)\))`

// // #GOGP_REPLACE(<src>,<dst>)
// expTxtReplaceKey = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_REPLACE\((?P<REPSRC>\S+)[ |\t]*,[ |\t]*(?P<REPDST>\S+)\))`
// expTxtMapKey = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_MAP\((?P<MAPSRC>\S+)[ |\t]*,[ |\t]*(?P<MAPDST>\S+)\))`

// //remove "*" from value type such as "*string -> string"
// // #GOGP_RAWNAME(<strValueType>)
// //gsExpTxtRawName = "(?-sm:(?://)?#GOGP_RAWNAME\((?P<RAWNAME>\S+)\))"

// // only generate <content> once from a gp file:
// // //#GOGP_ONCE <content> //#GOGP_END_ONCE
// expTxtOnce = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)//#GOGP_ONCE(?:[ |\t]*?//.*?$)?[\r|\n]*(?P<ONCE>.*?)[\r|\n]*[ |\t]*?(?://)??#GOGP_END_ONCE.*?$[\r|\n]*)`

// expTxtFileBegin = `(?sm:\s*(?P<FILEB>//#GOGP_FILE_BEGIN(?:[ |\t]+(?P<OPEN>[[:word:]]+))?).*?$[\r|\n]*(?://#GOGP_IGNORE_BEGIN ///gogp_file_begin.*?(?://)?#GOGP_IGNORE_END ///gogp_file_begin.*?$)?[\r|\n]*)`
// expTxtFileEnd = `(?sm:\s*(?P<FILEE>//#GOGP_FILE_END).*?$[\r|\n]*(?://#GOGP_IGNORE_BEGIN ///gogp_file_end.*?(?://)?#GOGP_IGNORE_END ///gogp_file_end.*?$)?[\r|\n]*)`

// gogpExpTodoReplace = regexp.MustCompile(expTxtTodoReplace)
// gogpExpPretreatAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s|%s|%s|%s", expTxtIgnore, expTxtRequire, expTxtGetGpgCfg, expTxtOnce, expTxtReplaceKey, expTxtGogpComment))
// gogpExpIgnore = regexp.MustCompile(expTxtIgnore)
// gogpExpCodeSelector = regexp.MustCompile(fmt.Sprintf("%s|%s|%s|%s|%s|%s", expTxtIgnore, expTxtGPOnly, expTxtIf, expTxtIf2, expTxtMapKey, expTxtSwitch))
// gogpExpCases = regexp.MustCompile(expTxtCase)
// gogpExpEmptyLine = regexp.MustCompile(expTxtEmptyLine)
// gogpExpTrimEmptyLine = regexp.MustCompile(expTxtTrimEmptyLine)
// gogpExpRequire = regexp.MustCompile(expTxtRequire)
// gogpExpRequireAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s", expTxtRequire, expTxtFileBegin, expTxtFileEnd))
// gogpExpReverseIgnoreAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s", expTxtFileBegin, expTxtFileEnd, expTxtIgnore))
// gogpExpCondition = regexp.MustCompile(expTxtRequire)
// gogpExpComment = regexp.MustCompile(expTxtGogpComment)
*/
71 changes: 0 additions & 71 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
package gogp

import (
"bytes"
"fmt"
"regexp"
"strings"
)

Expand All @@ -47,81 +44,13 @@ const (
rawKeyKeyType = "KEY_TYPE" //key_type
rawKeyValueType = "VALUE_TYPE" //value_type

// // #GOGP_COMMENT
expTxtGogpComment = `(?sm:(?P<COMMENT>/{2,}[ |\t]*#GOGP_COMMENT))`

//generic-programming flag <XXX>
expTxtTodoReplace = `(?P<P>.?)(?P<W>\<[[:alpha:]_][[:word:]]*\>)(?P<S>.?)`

// ignore all text format:
// //#GOGP_IGNORE_BEGIN <content> //#GOGP_IGNORE_END
expTxtIgnore = `(?sm:\s*//#GOGP_IGNORE_BEGIN(?P<IGNORE>.*?)(?://)??#GOGP_IGNORE_END.*?$[\r|\n]*)`
expTxtGPOnly = `(?sm:\s*//#GOGP_GPONLY_BEGIN(?P<GPONLY>.*?)(?://)??#GOGP_GPONLY_END.*?$[\r|\n]*)`

// select by condition <cd> defines in gpg file:
// //#GOGP_IFDEF <cd> <true_content> //#GOGP_ELSE <false_content> //#GOGP_ENDIF
// "<key> || ! <key> || <key> == xxx || <key> != xxx"
expTxtIf = `(?sm:^(?:[ |\t]*/{2,}[ |\t]*)#GOGP_IFDEF[ |\t]+(?P<CONDK>[[:word:]<>\|!= \t]+)(?:.*?$[\r|\n]?)(?P<T>.*?)(?:(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ELSE(?:.*?$[\r|\n]?)[\r|\n]*(?P<F>.*?))?(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ENDIF.*?$[\r|\n]?)`
expTxtIf2 = `(?sm:^(?:[ |\t]*/{2,}[ |\t]*)#GOGP_IFDEF2[ |\t]+(?P<CONDK2>[[:word:]<>\|!= \t]+)(?:.*?$[\r|\n]?)(?P<T2>.*?)(?:(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ELSE2(?:.*?$[\r|\n]?)[\r|\n]*(?P<F2>.*?))?(?:[ |\t]*/{2,}[ |\t]*)#GOGP_ENDIF2.*?$[\r|\n]?)`

// " <key> || !<key> || <key> == xxx || <key> != xxx "
// [<NOT>] <KEY> [<OP><VALUE>]
expCondition = `(?sm:^[ |\t]*(?P<NOT>!)?[ |\t]*(?P<KEY>[[:word:]<>]+)[ |\t]*(?:(?P<OP>==|!=)[ |\t]*(?P<VALUE>[[:word:]]+))?[ |\t]*)`

//#GOGP_SWITCH [<SWITCHKEY>] <CASES> #GOGP_GOGP_ENDSWITCH
expTxtSwitch = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)(?:#GOGP_SWITCH)(?:[ |\t]+(?P<SWITCHKEY>[[:word:]<>]+))?(?:[ |\t]*?.*?$)[\r|\n]*(?P<CASES>.*?)(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_ENDSWITCH.*?$[\r|\n]?)`

//#GOGP_CASE <COND> <CASE> #GOGP_ENDCASE
//#GOGP_DEFAULT <CASE> #GOGP_ENDCASE
expTxtCase = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)(?:(?:#GOGP_CASE[ |\t]+(?P<COND>[[:word:]<>\|!]+))|(?:#GOGP_DEFAULT))(?:[ |\t]*?.*?$)[\r|\n]*(?P<CASE>.*?)(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_ENDCASE.*?$[\r|\n]*)`

// require another gp file:
// //#GOGP_REQUIRE(<gpPath> [, <gpgSection>])
expTxtRequire = `(?sm:\s*(?P<REQ>^[ |\t]*(?://)?#GOGP_REQUIRE\((?P<REQP>[^\n\r,]*?)(?:[ |\t]*?,[ |\t]*?(?:(?P<REQN>[[:word:]|#|@]*)|#GOGP_GPGCFG\((?P<REQGPG>[[:word:]]+)\)))??(?:[ |\t]*?\))).*?$[\r|\n]*(?:(?://#GOGP_IGNORE_BEGIN )?///require begin from\([^\n\r,]*?\)(?P<REQCONTENT>.*?)(?://)?(?:#GOGP_IGNORE_END )?///require end from\([^\n\r,]*?\))?[\r|\n]*)`
expTxtEmptyLine = `(?sm:(?P<EMPTY_LINE>[\r|\n]{3,}))`

//must be "-sm", otherwise it with will repeat every line
expTxtTrimEmptyLine = `(?-sm:^[\r|\n]*(?P<CONTENT>.*?)[\r|\n]*$)`

// get gpg config string:
// #GOGP_GPGCFG(<cfgName>)
expTxtGetGpgCfg = `(?sm:(?://)?#GOGP_GPGCFG\((?P<GPGCFG>[[:word:]]+)\))`

// #GOGP_REPLACE(<src>,<dst>)
expTxtReplaceKey = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_REPLACE\((?P<REPSRC>\S+)[ |\t]*,[ |\t]*(?P<REPDST>\S+)\))`
expTxtMapKey = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)#GOGP_MAP\((?P<MAPSRC>\S+)[ |\t]*,[ |\t]*(?P<MAPDST>\S+)\))`

//remove "*" from value type such as "*string -> string"
// #GOGP_RAWNAME(<strValueType>)
//gsExpTxtRawName = "(?-sm:(?://)?#GOGP_RAWNAME\((?P<RAWNAME>\S+)\))"

// only generate <content> once from a gp file:
// //#GOGP_ONCE <content> //#GOGP_END_ONCE
expTxtOnce = `(?sm:(?:^[ |\t]*/{2,}[ |\t]*)//#GOGP_ONCE(?:[ |\t]*?//.*?$)?[\r|\n]*(?P<ONCE>.*?)[\r|\n]*[ |\t]*?(?://)??#GOGP_END_ONCE.*?$[\r|\n]*)`

expTxtFileBegin = `(?sm:\s*(?P<FILEB>//#GOGP_FILE_BEGIN(?:[ |\t]+(?P<OPEN>[[:word:]]+))?).*?$[\r|\n]*(?://#GOGP_IGNORE_BEGIN ///gogp_file_begin.*?(?://)?#GOGP_IGNORE_END ///gogp_file_begin.*?$)?[\r|\n]*)`
expTxtFileEnd = `(?sm:\s*(?P<FILEE>//#GOGP_FILE_END).*?$[\r|\n]*(?://#GOGP_IGNORE_BEGIN ///gogp_file_end.*?(?://)?#GOGP_IGNORE_END ///gogp_file_end.*?$)?[\r|\n]*)`

// "//#GOGP_IGNORE_BEGIN ... //#GOGP_IGNORE_END"
txtRequireResultFmt = "//#GOGP_IGNORE_BEGIN ///require begin from(%s)\n%s\n//#GOGP_IGNORE_END ///require end from(%s)"
txtRequireAtResultFmt = "///require begin from(%s)\n%s\n///require end from(%s)"
txtGogpIgnoreFmt = "//#GOGP_IGNORE_BEGIN%s%s//#GOGP_IGNORE_END%s"
)

var (
gogpExpTodoReplace = regexp.MustCompile(expTxtTodoReplace)
gogpExpPretreatAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s|%s|%s|%s", expTxtIgnore, expTxtRequire, expTxtGetGpgCfg, expTxtOnce, expTxtReplaceKey, expTxtGogpComment))
gogpExpIgnore = regexp.MustCompile(expTxtIgnore)
gogpExpCodeSelector = regexp.MustCompile(fmt.Sprintf("%s|%s|%s|%s|%s|%s", expTxtIgnore, expTxtGPOnly, expTxtIf, expTxtIf2, expTxtMapKey, expTxtSwitch))
gogpExpCases = regexp.MustCompile(expTxtCase)
gogpExpEmptyLine = regexp.MustCompile(expTxtEmptyLine)
gogpExpTrimEmptyLine = regexp.MustCompile(expTxtTrimEmptyLine)
gogpExpRequire = regexp.MustCompile(expTxtRequire)
gogpExpRequireAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s", expTxtRequire, expTxtFileBegin, expTxtFileEnd))
gogpExpReverseIgnoreAll = regexp.MustCompile(fmt.Sprintf("%s|%s|%s", expTxtFileBegin, expTxtFileEnd, expTxtIgnore))
gogpExpCondition = regexp.MustCompile(expTxtRequire)
gogpExpComment = regexp.MustCompile(expTxtGogpComment)

txtFileBeginContent = `//
/* //This line can be uncommented to disable all this file, and it doesn't effect to the .gp file
// //If test or change .gp file required, comment it to modify and compile as normal go file
Expand Down
2 changes: 1 addition & 1 deletion replace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (this *replaceList) expString() (exp string) {
exp = b.String()
} else {
//avoid return "", which will match every byte
exp = `\Q#GOGP_DO_NOT_HAVE_ANY_KEY#\E`
exp = `\Q#GOGP_DO_NOT_HAVE_ANY_REPLACE_KEY#\E`
}

//fmt.Println(exp)
Expand Down
Loading

0 comments on commit ca0f6cb

Please sign in to comment.