All Projects → rhysd → locerr

rhysd / locerr

Licence: MIT license
❌ locerr (locational error): Library for nice-looking errors in source code

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to locerr

Gradle License Plugin
Gradle plugin that provides a task to generate a HTML license report of your project.
Stars: ✭ 246 (+1437.5%)
Mutual labels:  source
slm-code-generation
TensorFlow code for the neural network presented in the paper: "Structural Language Models of Code" (ICML'2020)
Stars: ✭ 75 (+368.75%)
Mutual labels:  source
GitHubSpanishRankingGenerator
Scripts to build the GitHub Spanish rankings (users most active in each province sorted by public contributions) 🇪🇸
Stars: ✭ 19 (+18.75%)
Mutual labels:  source
arm-hard-fault-handler
What to do when Hard fault hits? Debugger and error reporter solution for ARM Cortex M3 and M4.
Stars: ✭ 32 (+100%)
Mutual labels:  error
HexTags
Customize tags & chat colors!
Stars: ✭ 53 (+231.25%)
Mutual labels:  source
Avalonia.PropertyGenerator
Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions.
Stars: ✭ 20 (+25%)
Mutual labels:  source
Develop Source
Open source for developer.(开发资源整理:Java,Android,算法,iOS,MacOS等等)
Stars: ✭ 219 (+1268.75%)
Mutual labels:  source
XcodeCommentWrapper
Xcode extension for wrapping comments
Stars: ✭ 29 (+81.25%)
Mutual labels:  source
error-handler
PSR-15 middleware to handle http errors
Stars: ✭ 13 (-18.75%)
Mutual labels:  error
custom-error-test
Compare and test various custom error implementations.
Stars: ✭ 32 (+100%)
Mutual labels:  error
get-source
Fetch source-mapped sources. Peek by file, line, column. Node & browsers. Sync & async.
Stars: ✭ 26 (+62.5%)
Mutual labels:  source
SaveVolley
Save volley from anything, By Agera to save. Thus, derived the AgeraVolley . (。>﹏<。)
Stars: ✭ 29 (+81.25%)
Mutual labels:  source
NetMauMau
Server for the popular card game Mau Mau (similar to UNO®)
Stars: ✭ 16 (+0%)
Mutual labels:  source
opensource
Collection of Open Source packages by Otherwise
Stars: ✭ 21 (+31.25%)
Mutual labels:  source
LegionTD-Reborn
A custom game mode inspired by Legion TD for DotA 2
Stars: ✭ 34 (+112.5%)
Mutual labels:  source
Pubg mobile memory hacking examples
Pubg Mobile Emulator Gameloop Memory Hacking C++ code examples. Ex: Name, Coord, Bones, Weapons, Items, Box, Drop etc.
Stars: ✭ 224 (+1300%)
Mutual labels:  source
react-native-easy-state-view
Fully customizable State View for React Native.
Stars: ✭ 21 (+31.25%)
Mutual labels:  error
Timeline
AS2 & AS3 CPPS Emulator, written in Python. (Club Penguin Private Server Emulator)
Stars: ✭ 49 (+206.25%)
Mutual labels:  source
try-to-catch
functional try-catch wrapper for promises
Stars: ✭ 30 (+87.5%)
Mutual labels:  error
Mat-O-Wahl
🇩🇪 Mat-O-Wahl - Ein einfach zu bedienender, freier Open Source Wahl-O-Mat Klon fuer jedermann ### 🇬🇧 🇺🇸 A simple to handle, free "Voting Advice Application" / "Electoral Compass" alternative
Stars: ✭ 27 (+68.75%)
Mutual labels:  source

locerr

Build Status Windows Build status Coverage Status GoDoc

locerr is a small library to make a nice-looking locational error in a source code. It provides a struct to represent a source file, a specific position in code and an error related to specific range or position in source.

This library is useful to provide a unified look for error messages raised by compilers, interpreters or translators.

By using locerr.Source and locerr.Pos types as position information, this library can provide an error type which shows nice look error message.

  • It shows the code snippet which caused an error
  • Enable to add notes to error by nesting an error instance like pkg/errors
  • Proper location is automatically added to error messages and notes
  • Colorized label like 'Error:' or 'Note:'
  • Windows is supported

It's important to make a good error when compilation or execution errors found. locerr helps it. This library is actually used in some my compiler implementation.

Installation

Please use go get.

$ go get -u github.com/rhysd/locerr

Usage

As example, let's say to make a locational error for following pseudo code. In this code, function foo is defined with 1 parameter but called with 3 parameters.

function foo(x: bool): int {
  return (if x then 42 else 21)
}

function main() {
  foo(true,
      42,
      "test")
}

We can make a locational error with some notes using locerr as following.

package main

import (
	"fmt"
	"os"

	"github.com/rhysd/locerr"
)

func main() {
	// At first you should gain entire source as *locerr.Source instance.

	code :=
`function foo(x: bool): int {
  return (if x then 42 else 21)
}

function main() {
  foo(true,
      42,
      "test")
}`
	src := locerr.NewDummySource(code)

	// You can get *locerr.Source instance from file (NewSourceFromFile) or stdin (NewSourceFromStdin) also.

	// Let's say to find an error at some range in the source. 'start' indicates the head of the first argument.
    // 'end' indicates the end of the last argument.

	start := locerr.Pos{
		Offset: 88,
		Line:   6,
		Column: 7,
		File:   src,
	}
	end := locerr.Pos{
		Offset: 116,
		Line:   9,
		Column: 12,
		File:   src,
	}

	// NewError or other factory functions make a new error instance with the range. locerr.Error instance
	// implements error interface so it can be handled like other error types.

	err := locerr.ErrorIn(start, end, "Calling 'foo' with wrong number of argument")

	// Assume that you find additional information (location of variable and its type). Then you can add some
	// notes to the error. Notes can be added by wrapping errors like pkg/errors library.

	prev := locerr.Pos{
		Offset: 9,
		Line:   1,
		Column: 10,
		File:   src,
	}

	err = err.NoteAt(prev, "Defined with 1 parameter")
	err = err.NoteAt(prev, "'foo' was defined as 'bool -> int'")

	// Finally you can see the result!

	// Get the error message as string. Note that this is only for non-Windows OS.
	fmt.Println(err)

	// Directly writes the error message into given file.
	// This supports Windows. Useful to output from stdout or stderr.
	err.PrintToFile(os.Stdout)
}

Above code should show the following output:

Error: Calling 'foo' with wrong number of argument (at <dummy>:6:7)
  Note: Defined with 1 parameter (at <dummy>:1:10)
  Note: 'foo' was defined as 'bool -> int' (at <dummy>:1:10)

>   foo(true,
>       42,
>       "test")

output screenshot

Labels such as 'Error:' or 'Notes:' are colorized. Main error message is emphasized with bold font. And source code location information (file name, line and column) is added with gray text. If the error has range information, the error shows code snippet which caused the error at the end of error message.

If you have only one position information rather than two, 'start' position and 'end' position, ErrorAt is available instead of ErrorIn. ErrorAt takes one Pos instance.

err := locerr.ErrorAt(start, "Calling 'foo' with wrong number of argument")

In this case, line snippet is shown in error message. pos.Line is used to get line from source text. fmt.Println(err) will show the following.

Error: Calling 'foo' with wrong number of argument (at <dummy>:6:7)

>   foo(true,

Development

How to run tests

$ go test ./

Note that go test -v may fail because color sequences are not assumed in tests.

How to run fuzzing test

Fuzzing test using go-fuzz.

$ cd ./fuzz
$ go-fuzz-build github.com/rhysd/locerr/fuzz
$ go-fuzz -bin=./locerr_fuzz-fuzz.zip -workdir=fuzz

Last command starts fuzzing tests until stopped with ^C. Every 3 seconds it reports the current result. It makes 3 directories in fuzz directory as the result, corpus, crashers and suppressions. crashers contains the information about the crash caused by fuzzing.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].