Code Monkey home page Code Monkey logo

Comments (4)

flyx avatar flyx commented on May 27, 2024

It's a good thing you use the lib, so that these errors surface. Keep reporting!

from nimyaml.

flyx avatar flyx commented on May 27, 2024

There is no easy solution for this. After abc, the parser starts reading the next line because it may be a multiline scalar. Upon encountering surname at the uppermost indentation level, it determines that the scalar does not continue here and yields the token abc. However, the lexer has already entered the next line and therefore, when the serialization queries the current line to report the error, it gets the line after abc. By design, BaseLexer may have already dumped the previous line.

Two possibilities to solve this:

  • Cache the starting line in the parser and preserve it until the token is finalized. This gravely cripples performance and is therefore not a viable solution.
  • Remove the lineContent field from the exception because it does not work properly. Instead, provide a utility proc, that, provided with the exception and the whole YAML source as Stream, renders the line with marker. This may not be optimal because it does not cover all use-cases (e.g. if you process YAML directly from an non-resettable data stream), but is probably the cleanest solution.

from nimyaml.

flyx avatar flyx commented on May 27, 2024

This is now fixed as follows:

  • if the parser is invoked with a Stream as source (like in the code given here),
    • if the lexer is still in the same line, it returns the line with marker.
    • if the lexer has already advanced to the next line, it returns the empty string.
  • If the parser is invoked with a string as source (new API), the line will always be correctly displayed.

Example:

import yaml

type Person = object
  name, surname: string
  age: int

var
  s = """name: Andrea
age: abc
surname: Ferretti"""
  c: Person
try:
  load(s, c)
except YamlConstructionError:
  let e = (ref YamlConstructionError)(getCurrentException())
  echo "ERROR at line ", e.line, " column ", e.column
  echo e.lineContent
  echo e.msg

will display:

ERROR at line 2 column 6
age: abc
     ^

Cannot construct to int32: abc

from nimyaml.

andreaferretti avatar andreaferretti commented on May 27, 2024

Great, thank you very much!

from nimyaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.