Documentation ¶
Overview ¶
Package strlit provides a way to "compile" a string literal into the value the it represents.
For example, here are some string different string literals:
'apple banana cherry\tgrape'
"apple banana cherry\tgrape"
‘apple banana cherry\tgrape’
“apple banana cherry\tgrape”
‹apple banana cherry\tgrape›
«apple banana cherry\tgrape»
Note that 6 different styles of quotes are supported by this package.
It also supports a number of escape sequences. Namely:
\0 (null)
\a (bell)
\b (backspace)
\e (escape)
\f (form feed)
\n (line feed)
\r (carriage return)
\t (horizontal tab )
\v (vertical tab)
\\ (backslash)
\'
\"
\?
\‘
\’
\“
\”
\‹
\›
\«
\»
\xhh
\uhhhh
\Uhhhhhhhh
Example ¶
An example usage of this package looks like the following.
stringLiteral := `"This is a string literal.\tStill going."` //stringLiteral := `'This is a string literal.\tStill going.'` //stringLiteral := `‘This is a string literal.\tStill going.’` //stringLiteral := `“This is a string literal.\tStill going.”` //stringLiteral := `‹This is a string literal.\tStill going.›` //stringLiteral := `«This is a string literal.\tStill going.»` compiled, code, err := strlit.Compile( strings.NewReader(stringLiteral) ) if nil != err { //@TODO } fmt.Printf("The value of the string literal is: %s\n", compiled.String()) fmt.Printf("The original pre-compiled string literal is: %s\n", code.String())
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
func Compile(runeScanner io.RuneScanner) (valueOfStringLiteral Parcel, stringLiteralCode Parcel, err error)
Compile will "compile" a string literal, which it pulls from the io.RunScanner, into the value the string literal represents.
It returns both the value of the string literal and the original string literal code.
If there is a syntax error in the string literal, it will return an error of type strlit.SyntaxErrorComplainer.
If your string literal is in a Go string, and you want to pass it to strlit.Compile(), then wrap it in a strings.NewReader(). For example:
compiled, code, err := strlit.Compile( strings.NewReader("‘apple banana cherry’") )
Types ¶
type Parcel ¶
Parcel is used for both containing the result of compiling a string literal, and for containing the original pre-compiled string literal code.
It provides the Bytes, Runes and String methods; used to retrieve the value of the string literal. in []byte, []rune and string formats, respectively.
type SyntaxErrorComplainer ¶
type SyntaxErrorComplainer interface { error // SyntaxErrorComplainer is used for typing. Calling it will do nothing. SyntaxErrorComplainer() // Code returns "code" from the string literal that caused the error. Code() string }
SyntaxErrorComplainer is an error that is used to represent a syntax error in a string literal.
SyntaxErrorComplainer allows one to respond to errors returned from strlit.Compile() in a more precise way. For example:
compiled, err := strlit.Compile(runeReader) if nil != err { switch err.(type) { case strlit.SyntaxErrorComplainer: //@TODO default: //@TODO } }
An error message can be returned from the Error() method. In addition to this, one can construct their own error message using the Code() method.
The Code() method returns "code" from the string literal that caused the error.