| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Gram.Serialize
Description
Serialization of Pattern Subject to gram notation.
This module provides functions to convert Pattern Subject data structures into gram notation text format. The serialization handles all aspects of gram notation including:
Documentation
toGram :: [Pattern Subject] -> String Source #
Serialize a list of Patterns to gram notation (newline-separated).
First-pattern rule: If the first pattern is anonymous (identity == Symbol ""),
has no labels, and has no elements, it is serialized as a bare root record
{k:v} or {}. Only the first pattern is considered; the same shape in
any other position is serialized with serializePattern.
Round-trip: fromGram s >>= toGram preserves a leading bare record as the
first element. With fromGramWithIds, the header pattern gets an assigned
identity and is no longer header-like, so it will serialize as a normal
pattern (e.g. (#1 {...})).
toGramWithHeader :: Map String Value -> [Pattern Subject] -> String Source #
Serialize a header record and a list of Patterns to gram notation.
The header is always emitted as a bare record {k:v} or {}, then a newline,
then toGram of the patterns. The header is explicit; the first-pattern rule
of toGram still applies to the pattern list (so a header-like first pattern
would also serialize as a bare record on the next line).
serializePattern :: Pattern Subject -> String Source #
Serialize a Pattern Subject to gram notation.
Converts a Pattern Subject data structure into its gram notation
string representation. The output follows the gram notation specification:
- Patterns with elements use subject syntax: `[attributes | elements]`
- Patterns without elements use node syntax: (attributes)
Examples
Simple node (no elements):
>>>import Pattern.Core (Pattern(..))>>>import Subject.Core (Subject(..), Symbol(..))>>>import Data.Set (Set)>>>import qualified Data.Set as Set>>>let s = Subject (Symbol "n") (Set.fromList ["Person"]) empty>>>let p = Pattern { value = s, elements = [] }>>>serializePattern p"(n:Person)"
Node with properties (no elements):
>>>import Data.Map (fromList)>>>import Subject.Value (VString)>>>let s = Subject (Symbol "n") (Set.fromList ["Person"]) (fromList [("name", VString "Alice")])>>>let p = Pattern { value = s, elements = [] }>>>serializePattern p"(n:Person {name:\"Alice\"})"
Subject with nested elements:
>>>let inner1 = Pattern (Subject (Symbol "a") Set.empty empty) []>>>let inner2 = Pattern (Subject (Symbol "b") Set.empty empty) []>>>let outer = Pattern (Subject (Symbol "g") Set.empty empty) [inner1, inner2]>>>serializePattern outer"[g | a, b]"
codefenceThreshold :: Int Source #
Character threshold for codefence serialization.
Strings with length greater than this value will be serialized using codefence format (triple-backticks). Length is measured as total character count including newline characters.
Strings of this length or fewer use standard quote-delimited format.
Examples
>>>codefenceThreshold120
>>>length "short string" <= codefenceThresholdTrue