yamlet
Safe HaskellNone
LanguageGHC2021

Yamlet.Error

Description

Errors with the position in the input that caused them.

Synopsis

Errors

data Error Source #

An error of the parser or the decoder.

An error from the functions of this module keeps no part of the input alive once it is in weak head normal form. Its message and its path are evaluated then, because a message often contains a slice of the input, e.g. a key, and a path comes from the syntax tree.

Constructors

Error 

Fields

  • location :: !Location
     
  • message :: !String
     
  • sourceLine :: !Text

    The line of the input that contains the location.

  • sourceIndex :: !Int

    The index of the location in the UTF-8 bytes of sourceLine. It lets prettyError find the column without a scan of the whole line.

  • path :: !Path

    The path to the node of a decoder error. An error at a key has the path of its mapping. The path is empty for an error of the parser and for a node that a program built.

Instances

Instances details
NFData Error Source # 
Instance details

Defined in Yamlet.Error

Methods

rnf :: Error -> ()

Generic Error Source # 
Instance details

Defined in Yamlet.Error

Associated Types

type Rep Error 
Instance details

Defined in Yamlet.Error

type Rep Error = D1 ('MetaData "Error" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) (C1 ('MetaCons "Error" 'PrefixI 'True) ((S1 ('MetaSel ('Just "location") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Location) :*: S1 ('MetaSel ('Just "message") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 String)) :*: (S1 ('MetaSel ('Just "sourceLine") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Just "sourceIndex") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "path") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Path)))))

Methods

from :: Error -> Rep Error x

to :: Rep Error x -> Error

Show Error Source # 
Instance details

Defined in Yamlet.Error

Methods

showsPrec :: Int -> Error -> ShowS

show :: Error -> String

showList :: [Error] -> ShowS

Eq Error Source # 
Instance details

Defined in Yamlet.Error

Methods

(==) :: Error -> Error -> Bool

(/=) :: Error -> Error -> Bool

type Rep Error Source # 
Instance details

Defined in Yamlet.Error

type Rep Error = D1 ('MetaData "Error" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) (C1 ('MetaCons "Error" 'PrefixI 'True) ((S1 ('MetaSel ('Just "location") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Location) :*: S1 ('MetaSel ('Just "message") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 String)) :*: (S1 ('MetaSel ('Just "sourceLine") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: (S1 ('MetaSel ('Just "sourceIndex") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "path") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Path)))))

data Location Source #

A position in the input. Lines and columns count from 1, and a column counts characters, not bytes. Line 0 and column 0 mean that the error has no position, e.g. because it comes from a node that a program built, or from a parsed node that is decoded without its input.

Constructors

Location 

Fields

Instances

Instances details
NFData Location Source # 
Instance details

Defined in Yamlet.Error

Methods

rnf :: Location -> ()

Generic Location Source # 
Instance details

Defined in Yamlet.Error

Associated Types

type Rep Location 
Instance details

Defined in Yamlet.Error

type Rep Location = D1 ('MetaData "Location" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) (C1 ('MetaCons "Location" 'PrefixI 'True) (S1 ('MetaSel ('Just "offset") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Offset) :*: (S1 ('MetaSel ('Just "line") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "column") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))))

Methods

from :: Location -> Rep Location x

to :: Rep Location x -> Location

Show Location Source # 
Instance details

Defined in Yamlet.Error

Methods

showsPrec :: Int -> Location -> ShowS

show :: Location -> String

showList :: [Location] -> ShowS

Eq Location Source # 
Instance details

Defined in Yamlet.Error

Methods

(==) :: Location -> Location -> Bool

(/=) :: Location -> Location -> Bool

type Rep Location Source # 
Instance details

Defined in Yamlet.Error

type Rep Location = D1 ('MetaData "Location" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) (C1 ('MetaCons "Location" 'PrefixI 'True) (S1 ('MetaSel ('Just "offset") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Offset) :*: (S1 ('MetaSel ('Just "line") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "column") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))))

data Path Source #

The keys and the indices from the root of a document to a node.

The path of a node shares the path of its parent, so the paths of many errors in a deep document take memory linear in its size.

Instances

Instances details
NFData Path Source # 
Instance details

Defined in Yamlet.Error

Methods

rnf :: Path -> ()

Show Path Source # 
Instance details

Defined in Yamlet.Error

Methods

showsPrec :: Int -> Path -> ShowS

show :: Path -> String

showList :: [Path] -> ShowS

Eq Path Source # 
Instance details

Defined in Yamlet.Error

Methods

(==) :: Path -> Path -> Bool

(/=) :: Path -> Path -> Bool

pathElements :: Path -> [PathElement] Source #

The steps of a path, from the root.

pathFromElements :: [PathElement] -> Path Source #

A path with the steps from the root.

data PathElement Source #

A step of a path into a document.

Constructors

Key !Text

The value of a key that is a scalar, with the text of the key, e.g. 1 for the integer key 1.

Index !Int

The item of a sequence, from 0.

CollectionKey

The value of a key that is a collection, e.g. ? [1, 2]. The path has no steps into the key.

AliasKey !Text

The value of a key that is an alias, with the name of the anchor.

Instances

Instances details
NFData PathElement Source # 
Instance details

Defined in Yamlet.Error

Methods

rnf :: PathElement -> ()

Generic PathElement Source # 
Instance details

Defined in Yamlet.Error

Associated Types

type Rep PathElement 
Instance details

Defined in Yamlet.Error

type Rep PathElement = D1 ('MetaData "PathElement" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) ((C1 ('MetaCons "Key" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "Index" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))) :+: (C1 ('MetaCons "CollectionKey" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "AliasKey" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))))

Methods

from :: PathElement -> Rep PathElement x

to :: Rep PathElement x -> PathElement

Show PathElement Source # 
Instance details

Defined in Yamlet.Error

Methods

showsPrec :: Int -> PathElement -> ShowS

show :: PathElement -> String

showList :: [PathElement] -> ShowS

Eq PathElement Source # 
Instance details

Defined in Yamlet.Error

Methods

(==) :: PathElement -> PathElement -> Bool

(/=) :: PathElement -> PathElement -> Bool

type Rep PathElement Source # 
Instance details

Defined in Yamlet.Error

type Rep PathElement = D1 ('MetaData "PathElement" "Yamlet.Error" "yamlet-1.0.0.1-2nRkbEnNUrLFBALTocq2AS" 'False) ((C1 ('MetaCons "Key" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "Index" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))) :+: (C1 ('MetaCons "CollectionKey" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "AliasKey" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))))

prettyError :: FilePath -> Error -> String Source #

Render an error in the format that editors recognize. The result does not end with a line break. If the line is longer than 80 characters, the excerpt shows only the 80 characters around the column. A control character other than a tab shows as its symbol, e.g. U+241B for escape, or as U+FFFD if Unicode has no symbol for it.

>>> either printErrors print (decodeText @(M.Map T.Text [[Int]]) "jobs:\n  - [1]\n  - 42\n")
input.yaml:3:5: jobs[1]: expected a list, but got an integer
  |
3 |   - 42
  |     ^

An error with no position gives only the file and the message, e.g. input.yaml: duplicate key "a".

renderPath :: Path -> String Source #

A path in the form jobs[1].name. A key that is a collection is ?, and a key that is an alias is its alias, e.g. *base.

A key is in double quotes, e.g. "a.b", if it:

  • is empty,
  • has white space, a character that cannot be printed, or one of the characters .[]"\,
  • starts with ? or *.

In the quotes, a character that cannot be printed has an escape as in YAML, e.g. "a\nb".

>>> renderPath (pathFromElements [Key "jobs", Index 1, Key "name"])
"jobs[1].name"
>>> renderPath (pathFromElements [Key "a.b", Key ""])
"\"a.b\".\"\""

Construction

errorAt :: Text -> Offset -> String -> Error Source #

Create an error at the given offset of the input.

errorsAt :: Text -> [(Offset, String)] -> [Error] Source #

Create errors at the given offsets of the input, in the order of the list. One scan of the input locates all of them, and the errors on one line share the copy of the line.

documentErrors :: Text -> Document -> [(Offset, String)] -> [Error] Source #

Create errors at the given offsets of a document, with their paths, in the order of the list. The text is the input of the document, e.g. for the offsets of Located values.

locate :: Text -> Offset -> Location Source #

Compute the line and the column of an offset. The byte order marks at the start of a line are not columns, because they are not content. For noOffset or an offset beyond the end of the input, the line and the column are 0.

nodePath :: Offset -> Node -> Path Source #

The path from the root to the node at the offset. If several nodes start there, e.g. a block mapping and its first key, the outermost one counts. A key does not add to the path, and a node inside a key that is a collection has the path of the mapping.

nodePaths :: [Offset] -> Node -> [Path] Source #

The paths of nodePath for several offsets, in the order of the offsets, from one walk of the tree.