strlit

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2016 License: MIT Imports: 3 Imported by: 0

README

go-strlit

A library that provides tools to "compile" a string literals into the value they represent, for the Go programming language.

This supports a number of different beginning and ending characters for string literals. Namely:

  • '...'
  • "..."
  • ‘...’
  • “...”
  • ‹...›
  • «...»

This also supports the following string literal escape sequences:

  • \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 (ex: \x5a)
  • \uhhhh (ex: \u005a)
  • \Uhhhhhhhh (ex: \U0000005a)

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-strlit

GoDoc

Example

  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())

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

type Parcel interface {
	Bytes() []byte
	Runes() []rune
	String() string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL