gram
Copyright(c) gram-data 2024
LicenseBSD3
Maintainergram-data
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

Gram.Parse

Description

This module provides parsers for gram notation, converting text into Pattern and Subject data structures.

Anonymous Subject Handling

The default fromGram function preserves anonymous subjects as 'Symbol ""' to enable round-trip compatibility. Anonymous subjects in gram notation (e.g., (), ()-[]->()) will be represented with empty identity.

If you need unique IDs assigned to anonymous subjects, use fromGramWithIds instead, which assigns sequential IDs (e.g., #1, #2) to all anonymous subjects.

String Value Syntax

The parser supports multiple string formats:

Double-quoted strings

Standard strings with escape sequences:

{ name: "Alice" }

Single-quoted strings

Literal strings without escape processing:

{ pattern: '[a-z]+' }

Codefence strings (multiline)

Triple-backtick delimited strings for multiline content:

{ content: ```
This content spans
multiple lines.
``` }

Tagged codefence strings

Codefence with a tag indicating the content type:

{ doc: ```md
# Markdown Title
Some **bold** text.
``` }

Tagged codefences are parsed as VTaggedString values with the tag and content stored separately.

Synopsis

Documentation

fromGram :: String -> Either ParseError [Pattern Subject] Source #

Parse gram notation into a list of Pattern Subject.

A leading bare record is represented as an anonymous, no-elements pattern (Pattern (Subject (Symbol "") Set.empty props) []) and placed first in the list. Empty or whitespace-only input yields Right [].

This function preserves anonymous subjects as 'Symbol ""' for round-trip compatibility. Use fromGramWithIds to assign generated IDs.

fromGramWithIds :: String -> Either ParseError [Pattern Subject] Source #

Parse gram notation string into a list of Pattern Subjects with ID assignment.

This function is equivalent to applying assignIdentities to each pattern in the result of fromGram. It assigns unique sequential IDs (e.g., #1, #2) to all anonymous subjects in the parsed patterns.

Use this function when you need unique identifiers for anonymous subjects, such as for graph algorithms or when distinguishing between anonymous instances is important.

For round-trip compatibility, use fromGram instead, which preserves anonymity.

fromGramWithHeader :: String -> Either ParseError (Maybe (Map String Value), [Pattern Subject]) Source #

Parse gram notation into an optional header and a list of patterns.

Returns (Maybe PropertyRecord, [Pattern Subject]). The leading bare record, if present, is only in the Maybe; it is not in the pattern list. Empty or whitespace-only input yields Right (Nothing, []).

data ParseError Source #

Parse error type for gram notation parsing.

Constructors

ParseError String 

Instances

Instances details
Show ParseError Source # 
Instance details

Defined in Gram.Parse

Eq ParseError Source # 
Instance details

Defined in Gram.Parse