jonquil_lexer Module

JSON lexer/tokenizer implementation.

This module provides the json_lexer type which tokenizes JSON input into a stream of tokens for the parser. The lexer extends the abstract lexer interface from TOML Fortran, inserting a prelude of tokens to wrap JSON data in a pseudo-TOML structure for compatibility with the TOML parser.

Supported JSON Tokens

  • Strings (double-quoted with escape sequences)
  • Numbers (integers and floating-point, including exponential notation)
  • Booleans (true, false)
  • Null (null)
  • Structural characters ({, }, [, ], :, ,)

Usage

The lexer is typically not used directly. Instead, use json_load or json_loads from the jonquil module.

type(json_lexer) :: lexer
call new_lexer_from_string(lexer, '{"key": 123}')


Derived Types

type, public, extends(abstract_lexer) ::  json_lexer

Tokenizer for JSON documents

Components

Type Visibility Attributes Name Initial
character(kind=tfc, len=:), public, allocatable :: chunk

Current source chunk

character(len=:), public, allocatable :: filename

Name of the source file, used for error reporting

integer, public :: pos = 0

Current internal position in the source chunk

integer, public :: prelude = 2

Additional tokens to insert before the actual token stream

Type-Bound Procedures

procedure, public :: extract_bool

Extract a boolean from a token

procedure, public :: extract_datetime

Extract a timestamp from a token

procedure, public :: extract_float

Extract a float from a token

procedure, public :: extract_integer

Extract an integer from a token

procedure, public :: extract_string

Extract a string from a token

procedure, public :: get_info

Get information about source

procedure, public :: next

Obtain the next token


Subroutines

public subroutine new_lexer_from_file(lexer, filename, error)

Create a new instance of a lexer by reading from a file

Arguments

Type IntentOptional Attributes Name
type(json_lexer), intent(out) :: lexer

Instance of the lexer

character(len=*), intent(in) :: filename

Name of the file to read from

type(toml_error), intent(out), allocatable :: error

Error code

public subroutine new_lexer_from_string(lexer, string)

Create a new instance of a lexer by reading from a string.

Arguments

Type IntentOptional Attributes Name
type(json_lexer), intent(out) :: lexer

Instance of the lexer

character(len=*), intent(in) :: string

String to read from

public subroutine new_lexer_from_unit(lexer, io, error)

Create a new instance of a lexer by reading from a unit.

Read more…

Arguments

Type IntentOptional Attributes Name
type(json_lexer), intent(out) :: lexer

Instance of the lexer

integer, intent(in) :: io

Unit to read from

type(toml_error), intent(out), allocatable :: error

Error code