Example #1
0
func CompileExpression(node lexer.ASTNode, scope Scope) (regexp Regexp, parentScope Scope, err error) {
	output := ""

	if !node.IsCommandType(lexer.CmdXExpression) {
		panic("Compile should be passed an XExpression node only")
	}

	// List of examples should be isolated in this expression's scope
	scope.CurrentRegexp = NewRegexp()
	output, scope, err = compileNodeChildren(node, scope)

	// The name of the regexp is the comment on the XExpression command
	scope.CurrentRegexp.TextName = node.Command().Comment
	scope.CurrentRegexp.Source = node.Line().String()

	// Run tests on the examples
	for _, example := range scope.CurrentRegexp.Examples {
		pass := example.Run(output)
		if pass {
			fmt.Fprintf(os.Stderr, " ✔  Match test passed on %s\n", example.Line.String())
		} else {
			fmt.Fprintf(os.Stderr, " ✕  Match test failed for\n\t%s\non %s\n", example.Text, example.Line.String())
		}
	}

	scope.CurrentRegexp.RegexpText = output

	return scope.CurrentRegexp, scope, err
}