// UsingByGroup emits tokens for the matched groups in the regex using a
// "sublexer". Used when lexing code blocks where the name of a sublexer is
// contained within the block, for example on a Markdown text block or SQL
// language block.
//
// The sublexer will be retrieved using sublexerGetFunc (typically
// internal.Get), using the captured value from the matched sublexerNameGroup.
//
// If sublexerGetFunc returns a non-nil lexer for the captured sublexerNameGroup,
// then tokens for the matched codeGroup will be emitted using the retrieved
// lexer. Otherwise, if the sublexer is nil, then tokens will be emitted from
// the passed emitter.
//
// Example:
//
// 	var Markdown = internal.Register(MustNewLexer(
// 		&Config{
// 			Name:      "markdown",
// 			Aliases:   []string{"md", "mkd"},
// 			Filenames: []string{"*.md", "*.mkd", "*.markdown"},
// 			MimeTypes: []string{"text/x-markdown"},
// 		},
// 		Rules{
// 			"root": {
// 				{"^(```)(\\w+)(\\n)([\\w\\W]*?)(^```$)",
// 					UsingByGroup(
// 						internal.Get,
// 						2, 4,
// 						String, String, String, Text, String,
// 					),
// 					nil,
// 				},
// 			},
// 		},
// 	))
//
// See the lexers/m/markdown.go for the complete example.
//
// Note: panic's if the number emitters does not equal the number of matched
// groups in the regex.
UsingByGroup emits tokens for the matched groups in the regex using a
"sublexer". Used when lexing code blocks where the name of a sublexer is
contained within the block, for example on a Markdown text block or SQL
language block.

The sublexer will be retrieved using sublexerGetFunc (typically
internal.Get), using the captured value from the matched sublexerNameGroup.

If sublexerGetFunc returns a non-nil lexer for the captured sublexerNameGroup,
then tokens for the matched codeGroup will be emitted using the retrieved
lexer. Otherwise, if the sublexer is nil, then tokens will be emitted from
the passed emitter.





See the lexers/m/markdown.go for the complete example.

panic's if the number emitters does not equal the number of matched
groups in the regex.
// UsingByGroup emits tokens for the matched groups in the regex using a // " sublexer " . Used when lexing code blocks where the name of a sublexer is // contained within the block for example on a Markdown text block or SQL // language block . // // The sublexer will be retrieved using sublexerGetFunc ( typically // internal . Get ) using the captured value from the matched sublexerNameGroup . // // If sublexerGetFunc returns a non - nil lexer for the captured sublexerNameGroup // then tokens for the matched codeGroup will be emitted using the retrieved // lexer . Otherwise if the sublexer is nil then tokens will be emitted from // the passed emitter . // // Example : // // var Markdown = internal . Register ( MustNewLexer ( // &Config { // Name : " markdown " // Aliases : [] string { " md " " mkd " } // Filenames : [] string { " * . md " " * . mkd " " * . markdown " } // MimeTypes : [] string { " text / x - markdown " } // } // Rules { // " root " : { // { " ^ ( ` ` ` ) ( \\ w + ) ( \\ n ) ( [ \\ w \\ W ] * ? ) ( ^ ` ` ` $ ) " // UsingByGroup ( // internal . Get // 2 4 // String String String Text String // ) // nil // } // } // } // )) // // See the lexers / m / markdown . go for the complete example . // // Note : panic ' s if the number emitters does not equal the number of matched // groups in the regex .
func UsingByGroup(sublexerGetFunc func(string) Lexer, sublexerNameGroup, codeGroup int, emitters ...Emitter) Emitter {
	return EmitterFunc(func(groups []string, lexer Lexer) Iterator {
		if len(emitters) != len(groups)-1 {
			panic("UsingByGroup expects number of emitters to be the same as len(groups)-1")
		}
		sublexer := sublexerGetFunc(groups[sublexerNameGroup])
		iterators := make([]Iterator, len(groups)-1)
		for i, group := range groups[1:] {
			if i == codeGroup-1 && sublexer != nil {
				var err error
				iterators[i], err = sublexer.Tokenise(nil, groups[codeGroup])
				if err != nil {
					panic(err)
				}
			} else {
				iterators[i] = emitters[i].Emit([]string{group}, lexer)
			}
		}
		return Concaterator(iterators...)
	})
}
func UsingByGroup ( sublexerGetFunc func ( string ) Lexer , sublexerNameGroup , codeGroup int , emitters ... Emitter ) Emitter { return EmitterFunc ( func ( groups [ ] string , lexer Lexer ) Iterator { if len ( emitters ) != len ( groups ) - 1 { panic ( " " ) } sublexer := sublexerGetFunc ( groups [ sublexerNameGroup ] ) iterators := make ( [ ] Iterator , len ( groups ) - 1 ) for i , group := range groups [ 1 : ] { if i == codeGroup - 1 && sublexer != nil { var err error iterators [ i ] , err = sublexer . Tokenise ( nil , groups [ codeGroup ] ) if err != nil { panic ( err ) } } else { iterators [ i ] = emitters [ i ] . Emit ( [ ] string { group } , lexer ) } } return Concaterator ( iterators ... ) } ) }
UsingByGroup emits tokens for the matched groups in the regex using a
"sublexer".
UsingByGroup emits tokens for the matched groups in the regex using a " sublexer " .
// bounds check // grab sublexer // build iterators