Example #1
0
func allocate() []*rubex.Regexp {
	// some overlap in here, but it'll make the parsing functions clearer
	matchers := make([]*rubex.Regexp, 0, NUM_LEXEMES)
	for _, p := range pattern {
		matchers = append(matchers, rubex.MustCompile(`\A`+p))
	}
	return matchers
}
Example #2
0
func (sr *RegexRewriter) Done() error {
	patterns := make([]string, 0, initSize)
	for _, cand := range sr.original {
		patterns = append(patterns, fmt.Sprintf("(%s)", cand))
	}
	sr.regex = rubex.MustCompile(strings.Join(patterns, "|"))
	return nil
}
Example #3
0
func init() {
	re1 = make([]Matcher, NUM)
	re2 = make([]Matcher, NUM)
	for i := 0; i < NUM; i++ {
		re1[i] = regexp.MustCompile("[a-c]*$")
		re2[i] = re.MustCompile("[a-c]*$")
	}
	TaskChann = make(chan *Task, 100)
	for i := 0; i < 10; i++ {
		STR += STR
	}
	println("len:", len(STR))
}
Example #4
0
func init() {
	// some overlap in here, but it'll make the parsing functions clearer
	pattern[SPACES] = `\s+`
	pattern[COMMA] = `\s*,`
	pattern[UNIVERSAL] = `\*`
	pattern[TYPE] = `[_a-zA-Z]\w*`
	pattern[ELEMENT] = `(\*|[_a-zA-Z]\w*)`
	pattern[CLASS] = `\.[-\w]+`
	pattern[ID] = `\#[-\w]+`
	pattern[LBRACKET] = `\[`
	pattern[RBRACKET] = `\]`
	pattern[ATTR_NAME] = `[-_:a-zA-Z][-\w:.]*`
	pattern[ATTR_VALUE] = `("(\\.|[^"\\])*"|'(\\.|[^'\\])*')`
	pattern[EQUALS] = `=`
	pattern[CONTAINS_CLASS] = `~=`
	pattern[DASH_PREFIXED] = `\|=`
	pattern[STARTS_WITH] = `\^=`
	pattern[ENDS_WITH] = `\$=`
	pattern[CONTAINS] = `\*=`
	pattern[MATCH_OP] = "(" + strings.Join([]string{pattern[EQUALS], pattern[CONTAINS_CLASS], pattern[DASH_PREFIXED], pattern[STARTS_WITH], pattern[ENDS_WITH], pattern[CONTAINS]}, "|") + ")"
	pattern[PSEUDO_CLASS] = `:[-a-z]+`
	pattern[FIRST_CHILD] = `:first-child`
	pattern[FIRST_OF_TYPE] = `:first-of-type`
	pattern[NTH_CHILD] = `:nth-child`
	pattern[NTH_OF_TYPE] = `:nth-of-type`
	pattern[ONLY_CHILD] = `:only-child`
	pattern[ONLY_OF_TYPE] = `:only-of-type`
	pattern[LAST_CHILD] = `:last-child`
	pattern[LAST_OF_TYPE] = `:last-of-type`
	pattern[NOT] = `:not`
	pattern[LPAREN] = `\s*\(`
	pattern[RPAREN] = `\s*\)`
	pattern[COEFFICIENT] = `[-+]?(\d+)?`
	pattern[SIGNED] = `[-+]?\d+`
	pattern[UNSIGNED] = `\d+`
	pattern[ODD] = `odd`
	pattern[EVEN] = `even`
	pattern[N] = `[nN]`
	pattern[OPERATOR] = `[-+]`
	pattern[PLUS] = `\+`
	pattern[MINUS] = `-`
	pattern[BINOMIAL] = strings.Join([]string{pattern[COEFFICIENT], pattern[N], `\s*`, pattern[OPERATOR], `\s*`, pattern[UNSIGNED]}, "")
	pattern[ADJACENT_TO] = `\s*\+`
	pattern[PRECEDES] = `\s*~`
	pattern[PARENT_OF] = `\s*>`
	pattern[ANCESTOR_OF] = `\s+`
	for i, p := range pattern {
		matcher[i] = rubex.MustCompile(`\A` + p)
	}
}
Example #5
0
func Generate(project *project.Project, mixer *tp.Mixer) (map[string][]byte, error) {
	// HACKY
	type TempRewriter struct {
		ToBeReplacedWithmatcher     string
		ToBeReplacedWithreplacement string
	}
	tmpRewriter := make([]TempRewriter, len(project.Rewriter.Host))
	for i, v := range project.Rewriter.Host {
		tmpRewriter[i].ToBeReplacedWithmatcher = v.Matcher
		tmpRewriter[i].ToBeReplacedWithreplacement = v.Replacement
	}
	// Make JSON
	data, err := json.Marshal(tmpRewriter)
	if err != nil {
		project.HostJson = "{}"
	} else {
		project.HostJson = string(data)
	}
	// Lowercase hack
	r := rubex.MustCompile("ToBeReplacedWithmatcher")
	project.HostJson = r.Gsub(project.HostJson, "matcher")
	r = rubex.MustCompile("ToBeReplacedWithreplacement")
	project.HostJson = r.Gsub(project.HostJson, "replacement")
	// Escape quotes
	r = rubex.MustCompile("\"")
	project.HostJson = r.Gsub(project.HostJson, "\\\"")

	// END HACKY

	segments := [4]string{"request.ts", "response_pre.ts", "body.ts", "response_post.ts"}
	var finalError error

	renderedSegments := make(map[string][]byte, 4)

	rewritersContainer := mixer
	// if mixer.GetPackagerVersion() > 0 {
	// 	rewritersContainer = project.HttpTransformers
	// 	if rewritersContainer == nil {
	// 		return nil, errors.New("Project does not contain any HTTP transformers.")
	// 	}
	// }

	for _, segment := range segments {
		rawTemplate, err := getRawTemplate(segment, rewritersContainer)

		if err != nil {
			panic(err)
		}

		renderedSegment, err := runTemplate(segment, rawTemplate, project)

		if err != nil {
			// Should really return an array of errors
			finalError = err
		} else {
			renderedSegments[segment] = renderedSegment
			if WriteRewritersToProjectDir {
				ioutil.WriteFile(filepath.Join(project.Path, project.ScriptPath, segment), renderedSegment, 0644)
			}
		}
	}

	return renderedSegments, finalError
}
Example #6
0
func init() {
	// Is there a more elegant way to do this?
	LexemeName[LPAREN] = "`(`"
	LexemeName[RPAREN] = "`)`"
	LexemeName[LBRACE] = "`{`"
	LexemeName[RBRACE] = "`}`"
	LexemeName[COMMA] = "`,`"
	LexemeName[DOT] = "`.`"
	LexemeName[EQUAL] = "`=`"
	LexemeName[PLUS] = "`+`"
	LexemeName[STRING] = "string literal"
	LexemeName[REGEXP] = "regular expression"
	LexemeName[POS] = "position keyword"
	LexemeName[GVAR] = "global variable"
	LexemeName[LVAR] = "local variable"
	LexemeName[KWD] = "keyword argument"
	LexemeName[ID] = "identifier"
	LexemeName[NAMESPACE] = "`@namespace` directive"
	LexemeName[OPEN] = "`@include` directive"
	LexemeName[FUNC] = "`@func` directive"
	LexemeName[TYPE] = "type name"
	LexemeName[PATH] = "path"
	LexemeName[IMPORT] = "`@import` directive"
	LexemeName[OPTIONAL] = "`@optional` directive"
	LexemeName[READ] = "`read` macro"
	LexemeName[EOF] = "end of file"
	LexemeName[ERROR] = "lexical error"

	matcher[STRING] = rubex.MustCompile(`\A"(\\.|[^"\\])*"|\A'(\\.|[^'\\])*'`)

	// the pattern and options of the regexp matches are in captures 1 and 3
	//regexpSlashPattern, _ = rubex.Compile(`\A\/((\\.|[^\/\\])*)\/([imxouesn]*)`)
	regexpSlashPattern = rubex.MustCompile(`\A/((\\.|[^/\\])*)/([imxouesn]*)`)
	regexpBackQuotePattern = rubex.MustCompile("\\A`((\\\\.|[^\\`\\\\])*)`([imxouesn]*)")
	matcher[POS] = rubex.MustCompile(`\A(top|bottom|before|after)`)
	matcher[GVAR] = rubex.MustCompile(`\A\$\w+`)
	matcher[LVAR] = rubex.MustCompile(`\A%\w+`)
	matcher[KWD] = rubex.MustCompile(`\A[a-zA-Z_:][-\w:.]*:`)
	matcher[ID] = rubex.MustCompile(`\A\$+|\A[_a-z][\w\$]*`)
	matcher[TYPE] = rubex.MustCompile(`\A[A-Z]\w*`)
	matcher[PATH] = rubex.MustCompile(`\A[-+.*?:\/\w]+`)

	// Map parens, braces, etc to their lexemes
	symbolLexeme = make(map[string]Lexeme, 8)
	symbolLexeme["("] = LPAREN
	symbolLexeme[")"] = RPAREN
	symbolLexeme["{"] = LBRACE
	symbolLexeme["}"] = RBRACE
	symbolLexeme[","] = COMMA
	symbolLexeme["."] = DOT
	symbolLexeme["="] = EQUAL
	symbolLexeme["+"] = PLUS
	symbolPattern = rubex.MustCompile(`\A[\(\)\{\}\,\.=\+]`)
	numberPattern = rubex.MustCompile(`\A\d+`)
}