5711 lines
192 KiB
C
5711 lines
192 KiB
C
/**
|
|
* @PROJECT AerScript Interpreter
|
|
* @COPYRIGHT See COPYING in the top level directory
|
|
* @FILE modules/yaml/yaml.c
|
|
* @DESCRIPTION Typed YAML DOM module for AerScript Interpreter
|
|
* @DEVELOPERS Symisc Systems <devel@symisc.net>
|
|
* Rafal Kupiec <belliash@codingworkshop.eu.org>
|
|
*/
|
|
#include <math.h>
|
|
#include <locale.h>
|
|
#include <string.h>
|
|
#ifdef YAML_DEBUG_PROGRESS
|
|
#include <assert.h>
|
|
#endif
|
|
#include "yaml.h"
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Known limitations (each has a tests/errors/yaml_* fixture) */
|
|
/* ------------------------------------------------------------------ */
|
|
/* 1. Complex mapping keys ('? [a, b]', '{[a]: v}') are unsupported:
|
|
* UNSUPPORTED=14, "Complex mapping keys are not supported".
|
|
* 2. AllowAliases=false rejects '&' and '*' with ALIAS_NOT_ALLOWED=15.
|
|
* 3. Parse()/YamlDocument.Parse() accept one document: a second '---' raises
|
|
* INVALID_OPERATION=8 with the stage-0 message and its own position.
|
|
* 4. Unknown tags (including global tags expanded via %TAG) raise
|
|
* UNKNOWN_TAG=13 under UNKNOWN_TAG_REJECT; UNKNOWN_TAG_KEEP stores the
|
|
* full URI, which the serializer emits as !<uri>.
|
|
* 5. Duplicate keys under DUP_REJECT raise DUPLICATE_KEY=7 at the second key.
|
|
* 6. MaxAliasExpansion counts nodes created by alias copies; exceeding it
|
|
* raises ALIAS_LIMIT=12 at the alias position.
|
|
* 7. Nesting past MaxDepth raises DEPTH=1 with the node path.
|
|
* 8. Any leading '!' on a flow mapping key is rejected (plain !name,
|
|
* !<uri> and !!std alike): flow keys are literal scalars handled by
|
|
* YamlParseScalarText, so tags never expand there. UNSUPPORTED=14,
|
|
* "Tags on flow mapping keys are not supported", at the key position.
|
|
* 9. Only %TAG and %YAML with an exact name match (the next byte must be
|
|
* white space, a line break or EOF) are special: %TAGGING/%YAMLX are
|
|
* ignored like any other unknown directive per YAML 1.2.2 6.8. Malformed
|
|
* %YAML (version digit '.' digit) and bare/malformed %TAG raise SYNTAX=2
|
|
* with a 1-based position.
|
|
* %TAG handles ARE supported and expanded; the DOM does not retain the source
|
|
* flow/block style or comments, so the serializer emits canonical block style. */
|
|
|
|
/* Class names used by the DOM. */
|
|
#define YCLS_EXC "YamlException"
|
|
#define YCLS_NODE "YamlNode"
|
|
#define YCLS_MAPPING "YamlMappingNode"
|
|
#define YCLS_SEQUENCE "YamlSequenceNode"
|
|
#define YCLS_STRING "YamlStringNode"
|
|
#define YCLS_INT "YamlIntNode"
|
|
#define YCLS_FLOAT "YamlFloatNode"
|
|
#define YCLS_BOOL "YamlBooleanNode"
|
|
#define YCLS_NULL "YamlNullNode"
|
|
#define YCLS_DOC "YamlDocument"
|
|
#define YCLS_FACADE "Yaml"
|
|
#define YCLS_OPTIONS "YamlOptions"
|
|
#define YCLS_KIND "YamlNodeType"
|
|
|
|
#define YAML_DEFAULT_MAX_DEPTH 512
|
|
#define YAML_DEFAULT_MAX_ALIAS_EXPANSION 10000
|
|
#define YAML_MAX_ALIAS_EXPANSION_LIMIT 1000000
|
|
|
|
/* Native type shorthands. */
|
|
#define YTY(T) { PH7_TYPE_##T, 0, 0 }
|
|
#define YTYARR(T) { PH7_TYPE_##T, 0, 1 }
|
|
#define YNM(N) { PH7_TYPE_NAMED, N, 0 }
|
|
#define YAR(N) { PH7_TYPE_NAMED, N, 1 }
|
|
#define YP(N,T) { N, YTY(T), 0, 0, 0, 0 }
|
|
#define YPN(N,X) { N, X, 0, 0, 0, 0 }
|
|
#define YPO(N,X,D) { N, X, 0, D, 0, 0 }
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Direct field access */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
/* Return the live slot of an instance attribute without a scope check.
|
|
* DOM helpers run from callbacks declared in several different classes;
|
|
* they all need to reach YamlNode's private fields. */
|
|
static ph7_value *YamlField(ph7_value *pObj, const char *zName) {
|
|
ph7_class_instance *pInst;
|
|
SyString s;
|
|
if(pObj == 0 || (pObj->nType & MEMOBJ_OBJ) == 0 || pObj->x.pOther == 0) {
|
|
return 0;
|
|
}
|
|
pInst = (ph7_class_instance *)pObj->x.pOther;
|
|
SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName));
|
|
return PH7_ClassInstanceFetchAttr(pInst, &s);
|
|
}
|
|
static ph7_int64 YamlFieldInt(ph7_value *pObj, const char *zName) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_to_int64(p) : 0;
|
|
}
|
|
static int YamlFieldBool(ph7_value *pObj, const char *zName) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_to_bool(p) : 0;
|
|
}
|
|
static const char *YamlFieldStr(ph7_value *pObj, const char *zName, int *pLen) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
if(p == 0) {
|
|
if(pLen) *pLen = 0;
|
|
return "";
|
|
}
|
|
return ph7_value_to_string(p, pLen);
|
|
}
|
|
static double YamlFieldDouble(ph7_value *pObj, const char *zName) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_to_double(p) : 0.0;
|
|
}
|
|
static int YamlFieldSetInt(ph7_value *pObj, const char *zName, ph7_int64 iVal) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_int64(p, iVal) : PH7_CORRUPT;
|
|
}
|
|
static int YamlFieldSetBool(ph7_value *pObj, const char *zName, int bVal) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_bool(p, bVal) : PH7_CORRUPT;
|
|
}
|
|
static int YamlFieldSetDouble(ph7_value *pObj, const char *zName, double rVal) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return p ? ph7_value_double(p, rVal) : PH7_CORRUPT;
|
|
}
|
|
static int YamlFieldSetStr(ph7_value *pObj, const char *zName, const char *zVal, int nByte) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
char *zCopy;
|
|
sxi32 rc;
|
|
if(p == 0) {
|
|
return PH7_CORRUPT;
|
|
}
|
|
if(zVal == 0) {
|
|
zVal = "";
|
|
nByte = 0;
|
|
}
|
|
if(nByte < 0) {
|
|
nByte = (int)SyStrlen(zVal);
|
|
}
|
|
/* ph7_value_string() appends to an existing string blob, and callers such
|
|
* as YamlRepath() pass a pointer into the very field being replaced. Copy
|
|
* the source before releasing the destination. */
|
|
zCopy = (char *)SyMemBackendAlloc(&pObj->pVm->sAllocator, (sxu32)nByte + 1);
|
|
if(zCopy == 0) {
|
|
return PH7_NOMEM;
|
|
}
|
|
memcpy(zCopy, zVal, (sxu32)nByte);
|
|
zCopy[nByte] = '\0';
|
|
PH7_MemObjRelease(p);
|
|
PH7_MemObjInit(pObj->pVm, p);
|
|
rc = ph7_value_string(p, zCopy, nByte);
|
|
SyMemBackendFree(&pObj->pVm->sAllocator, zCopy);
|
|
return rc;
|
|
}
|
|
/* Replace a container/object slot with a snapshot of the source value.
|
|
* Snapshot reserves arena members, so reacquire the destination slot before
|
|
* writing the owned copy back. */
|
|
static int YamlFieldSetObj(ph7_value *pObj, const char *zName, ph7_value *pSrc) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
ph7_value sTmp;
|
|
sxi32 rc;
|
|
if(p == 0 || pSrc == 0) {
|
|
return PH7_CORRUPT;
|
|
}
|
|
PH7_MemObjInit(pObj->pVm, &sTmp);
|
|
rc = PH7_MemObjSnapshot(pSrc, &sTmp);
|
|
if(rc != SXRET_OK) {
|
|
PH7_MemObjRelease(&sTmp);
|
|
return rc;
|
|
}
|
|
p = YamlField(pObj, zName);
|
|
if(p == 0) {
|
|
PH7_MemObjRelease(&sTmp);
|
|
return PH7_CORRUPT;
|
|
}
|
|
PH7_MemObjRelease(p);
|
|
PH7_MemObjInit(pObj->pVm, p);
|
|
rc = PH7_MemObjStore(&sTmp, p);
|
|
PH7_MemObjRelease(&sTmp);
|
|
return rc;
|
|
}
|
|
static ph7_value *YamlFieldContainer(ph7_value *pObj, const char *zName) {
|
|
ph7_value *p = YamlField(pObj, zName);
|
|
return (p && (p->nType & MEMOBJ_HASHMAP)) ? p : 0;
|
|
}
|
|
|
|
/* Defined with the parser block; used by the DOM walkers below. */
|
|
static const char *YamlKeyDecode(ph7_value *pKey, int *pLen);
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* YamlException support */
|
|
/* ------------------------------------------------------------------ */
|
|
static const char *YamlErrorDescription(int nErr) {
|
|
switch(nErr) {
|
|
case YAML_ERROR_NONE: return "No error";
|
|
case YAML_ERROR_DEPTH: return "Maximum depth exceeded";
|
|
case YAML_ERROR_SYNTAX: return "Syntax error";
|
|
case YAML_ERROR_ENCODING: return "Invalid encoding";
|
|
case YAML_ERROR_TYPE_MISMATCH: return "Type mismatch";
|
|
case YAML_ERROR_KEY_NOT_FOUND: return "Key not found";
|
|
case YAML_ERROR_INDEX_OUT_OF_RANGE: return "Index out of range";
|
|
case YAML_ERROR_DUPLICATE_KEY: return "Duplicate key";
|
|
case YAML_ERROR_INVALID_OPERATION: return "Invalid operation";
|
|
case YAML_ERROR_INVALID_OPTION: return "Invalid option";
|
|
case YAML_ERROR_READONLY_OPTION: return "Options are read-only";
|
|
case YAML_ERROR_UNDEFINED_ALIAS: return "Undefined alias";
|
|
case YAML_ERROR_ALIAS_LIMIT: return "Alias expansion limit exceeded";
|
|
case YAML_ERROR_UNKNOWN_TAG: return "Unknown tag";
|
|
case YAML_ERROR_UNSUPPORTED: return "Unsupported construct";
|
|
case YAML_ERROR_ALIAS_NOT_ALLOWED: return "Aliases are not allowed";
|
|
default: return "Unknown error code";
|
|
}
|
|
}
|
|
/* Fetch an inherited (protected) attribute slot straight off the instance.
|
|
* PH7_ClassInstanceFetchAttr walks the attribute table without a scope check,
|
|
* so the base Exception fields are reachable from this module. */
|
|
static ph7_value *YamlInstField(ph7_class_instance *pInst, const char *zName) {
|
|
SyString s;
|
|
if(pInst == 0) {
|
|
return 0;
|
|
}
|
|
SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName));
|
|
return PH7_ClassInstanceFetchAttr(pInst, &s);
|
|
}
|
|
/* Stamp the inherited Exception file/line fields with the first script frame
|
|
* of the active call stack. Native frames ([NATIVE], line 0) are skipped so
|
|
* catch(Exception $e) observes the place in the script that raised the error. */
|
|
static void YamlStampSite(ph7_vm *pVm, ph7_class_instance *pInst) {
|
|
SySet sTrace;
|
|
VmDebugTrace *pTrace;
|
|
ph7_value *pField;
|
|
if(VmExtractDebugTrace(pVm, &sTrace) != SXRET_OK) {
|
|
return;
|
|
}
|
|
while(SySetGetNextEntry(&sTrace, (void **)&pTrace) == SXRET_OK) {
|
|
if(pTrace->pFile == 0 || pTrace->nLine == 0) {
|
|
continue;
|
|
}
|
|
if(pTrace->pFile->nByte == sizeof("[NATIVE]") - 1 &&
|
|
SyMemcmp(pTrace->pFile->zString, "[NATIVE]", sizeof("[NATIVE]") - 1) == 0) {
|
|
continue;
|
|
}
|
|
pField = YamlInstField(pInst, "file");
|
|
if(pField) {
|
|
PH7_MemObjRelease(pField);
|
|
PH7_MemObjInit(pVm, pField);
|
|
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
|
|
}
|
|
pField = YamlInstField(pInst, "line");
|
|
if(pField) {
|
|
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
|
|
}
|
|
break;
|
|
}
|
|
SySetRelease(&sTrace);
|
|
}
|
|
/* Throw a real YamlException object; input line/column/path are carried verbatim. */
|
|
static int YamlThrow(ph7_context *pCtx, int nErr, const char *zMsg,
|
|
sxu32 nLine, sxu32 nCol, const char *zPath) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pClass;
|
|
ph7_class_instance *pInst;
|
|
ph7_class_method *pCons;
|
|
ph7_value *pObj, *apArg[5], *aTmp[5];
|
|
sxi32 rc;
|
|
sxu32 n;
|
|
if(pVm->iPendingTransfer) {
|
|
return PH7_EXCEPTION;
|
|
}
|
|
pClass = PH7_VmExtractClass(pVm, YCLS_EXC, sizeof(YCLS_EXC) - 1, FALSE);
|
|
if(pClass == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
|
|
aTmp[n] = ph7_context_new_scalar(pCtx);
|
|
if(aTmp[n] == 0) {
|
|
while(n > 0) {
|
|
--n;
|
|
ph7_context_release_value(pCtx, aTmp[n]);
|
|
}
|
|
PH7_ClassInstanceUnref(pInst);
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
}
|
|
pObj = ph7_context_new_scalar(pCtx);
|
|
if(pObj == 0) {
|
|
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
|
|
ph7_context_release_value(pCtx, aTmp[n]);
|
|
}
|
|
PH7_ClassInstanceUnref(pInst);
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
ph7_value_string(aTmp[0], zMsg, -1);
|
|
ph7_value_int64(aTmp[1], nErr);
|
|
ph7_value_int64(aTmp[2], (ph7_int64)nLine);
|
|
ph7_value_int64(aTmp[3], (ph7_int64)nCol);
|
|
ph7_value_string(aTmp[4], zPath ? zPath : "", -1);
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apArg) ; ++n) {
|
|
apArg[n] = aTmp[n];
|
|
}
|
|
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
|
|
if(pCons) {
|
|
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 5, apArg);
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
|
|
ph7_context_release_value(pCtx, aTmp[n]);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pObj);
|
|
PH7_ClassInstanceUnref(pInst);
|
|
return rc;
|
|
}
|
|
} else {
|
|
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
|
|
ph7_context_release_value(pCtx, aTmp[n]);
|
|
}
|
|
}
|
|
/* Base Exception fields describe the throwing script, not the YAML input. */
|
|
YamlStampSite(pVm, pInst);
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
PH7_MemObjRelease(pObj);
|
|
pObj->x.pOther = pInst;
|
|
MemObjSetType(pObj, MEMOBJ_OBJ);
|
|
rc = ph7_result_value(pCtx, pObj);
|
|
ph7_context_release_value(pCtx, pObj);
|
|
return rc == PH7_OK ? PH7_EXCEPTION : rc;
|
|
}
|
|
static int YamlThrowOption(ph7_context *pCtx, int nErr, const char *zMsg) {
|
|
return YamlThrow(pCtx, nErr, zMsg, 0, 0, "");
|
|
}
|
|
static int YamlThrowFmt(ph7_context *pCtx, int nErr, const char *zFmt, int iVal) {
|
|
char zBuf[256];
|
|
SyBufferFormat(zBuf, sizeof(zBuf), zFmt, iVal);
|
|
return YamlThrowOption(pCtx, nErr, zBuf);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* YamlOptions configuration */
|
|
/* ------------------------------------------------------------------ */
|
|
typedef struct yaml_opt yaml_opt;
|
|
struct yaml_opt {
|
|
int allowUnicode;
|
|
int allowAliases;
|
|
int maxAliasExpansion;
|
|
int duplicateKeyPolicy;
|
|
int unknownTagPolicy;
|
|
int schema;
|
|
int explicitDocumentStart;
|
|
int explicitDocumentEnd;
|
|
int style;
|
|
int maxDepth;
|
|
int indentWidth;
|
|
};
|
|
static void YamlOptDefaults(yaml_opt *p) {
|
|
SyZero(p, sizeof(*p));
|
|
p->allowUnicode = 1;
|
|
p->allowAliases = 1;
|
|
p->maxAliasExpansion = YAML_DEFAULT_MAX_ALIAS_EXPANSION;
|
|
p->duplicateKeyPolicy = YAML_DUP_REJECT;
|
|
p->unknownTagPolicy = YAML_UNKNOWN_TAG_REJECT;
|
|
p->schema = YAML_SCHEMA_CORE;
|
|
p->maxDepth = YAML_DEFAULT_MAX_DEPTH;
|
|
p->indentWidth = 2;
|
|
p->style = YAML_STYLE_BLOCK;
|
|
}
|
|
/* Validate an options instance. This is a pure test: it never mutates the
|
|
* instance. All diagnostics use input line=0, input column=0, path="". */
|
|
static int YamlOptValidate(ph7_context *pCtx, ph7_value *pOptions) {
|
|
int nMax, nAlias, nPolicy, nTag, nSchema, nIndent, nStyle;
|
|
if(pOptions == 0 || !ph7_value_is_object(pOptions)) {
|
|
return PH7_CORRUPT;
|
|
}
|
|
nMax = (int)YamlFieldInt(pOptions, "maxDepth");
|
|
if(nMax < 1 || nMax > YAML_DEFAULT_MAX_DEPTH) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.MaxDepth must be in range 1..512 (got %d)", nMax);
|
|
}
|
|
nAlias = (int)YamlFieldInt(pOptions, "maxAliasExpansion");
|
|
if(nAlias < 1 || nAlias > YAML_MAX_ALIAS_EXPANSION_LIMIT) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.MaxAliasExpansion must be in range 1..1000000 (got %d)", nAlias);
|
|
}
|
|
nPolicy = (int)YamlFieldInt(pOptions, "duplicateKeyPolicy");
|
|
if(nPolicy != YAML_DUP_KEEP_LAST && nPolicy != YAML_DUP_KEEP_FIRST && nPolicy != YAML_DUP_REJECT) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.DuplicateKeyPolicy must be YamlOptions.KEEP_LAST, KEEP_FIRST or REJECT (got %d)",
|
|
nPolicy);
|
|
}
|
|
nTag = (int)YamlFieldInt(pOptions, "unknownTagPolicy");
|
|
if(nTag != YAML_UNKNOWN_TAG_REJECT && nTag != YAML_UNKNOWN_TAG_KEEP) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.UnknownTagPolicy must be YamlOptions.UNKNOWN_TAG_REJECT or UNKNOWN_TAG_KEEP (got %d)",
|
|
nTag);
|
|
}
|
|
nSchema = (int)YamlFieldInt(pOptions, "schema");
|
|
if(nSchema != YAML_SCHEMA_CORE && nSchema != YAML_SCHEMA_JSON && nSchema != YAML_SCHEMA_FAILSAFE) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Schema must be YamlOptions.SCHEMA_CORE, SCHEMA_JSON or SCHEMA_FAILSAFE (got %d)",
|
|
nSchema);
|
|
}
|
|
nStyle = (int)YamlFieldInt(pOptions, "style");
|
|
if(nStyle != YAML_STYLE_BLOCK && nStyle != YAML_STYLE_FLOW) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Style must be YamlOptions.STYLE_BLOCK or STYLE_FLOW (got %d)", nStyle);
|
|
}
|
|
nIndent = (int)YamlFieldInt(pOptions, "indent");
|
|
if(nIndent < 2 || nIndent > 9) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Indent must be an integer in range 2..9 (got %d)", nIndent);
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
/* Validate, mark read-only on first use, then copy the scalar option values
|
|
* out of the instance. */
|
|
static int YamlOptLoad(ph7_context *pCtx, ph7_value *pOptions, yaml_opt *pOut) {
|
|
int rc;
|
|
/* Explicit null means "use the defaults": a bare null, or a YamlOptions-
|
|
* typed null, which the VM represents as an object type with no instance.
|
|
* YamlOptValidate itself stays strict for the public Validate() method. */
|
|
if(pOptions == 0 || (pOptions->nType & MEMOBJ_NULL) ||
|
|
!ph7_value_is_object(pOptions) || pOptions->x.pOther == 0) {
|
|
YamlOptDefaults(pOut);
|
|
return PH7_OK;
|
|
}
|
|
rc = YamlOptValidate(pCtx, pOptions);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
/* First consuming call seals the instance: later setters are rejected. */
|
|
(void)YamlFieldSetBool(pOptions, "readOnly", 1);
|
|
YamlOptDefaults(pOut);
|
|
pOut->allowUnicode = YamlFieldBool(pOptions, "allowUnicode");
|
|
pOut->allowAliases = YamlFieldBool(pOptions, "allowAliases");
|
|
pOut->maxAliasExpansion = (int)YamlFieldInt(pOptions, "maxAliasExpansion");
|
|
pOut->duplicateKeyPolicy = (int)YamlFieldInt(pOptions, "duplicateKeyPolicy");
|
|
pOut->unknownTagPolicy = (int)YamlFieldInt(pOptions, "unknownTagPolicy");
|
|
pOut->schema = (int)YamlFieldInt(pOptions, "schema");
|
|
pOut->explicitDocumentStart = YamlFieldBool(pOptions, "explicitDocumentStart");
|
|
pOut->explicitDocumentEnd = YamlFieldBool(pOptions, "explicitDocumentEnd");
|
|
pOut->style = (int)YamlFieldInt(pOptions, "style");
|
|
pOut->maxDepth = (int)YamlFieldInt(pOptions, "maxDepth");
|
|
pOut->indentWidth = (int)YamlFieldInt(pOptions, "indent");
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Node construction and path helpers */
|
|
/* ------------------------------------------------------------------ */
|
|
static ph7_value *YamlNewNode(ph7_context *pCtx, const char *zClass, int nKind, const char *zPath) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pClass = PH7_VmExtractClass(pVm, zClass, (sxu32)SyStrlen(zClass), FALSE);
|
|
ph7_class_instance *pInst;
|
|
ph7_value *pObj, *pArray;
|
|
if(pClass == 0) {
|
|
return 0;
|
|
}
|
|
pObj = ph7_context_new_scalar(pCtx);
|
|
if(pObj == 0) {
|
|
return 0;
|
|
}
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
ph7_context_release_value(pCtx, pObj);
|
|
PH7_VmMemoryError(pVm);
|
|
return 0;
|
|
}
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
PH7_MemObjRelease(pObj);
|
|
pObj->x.pOther = pInst;
|
|
MemObjSetType(pObj, MEMOBJ_OBJ);
|
|
YamlFieldSetInt(pObj, "kind", nKind);
|
|
YamlFieldSetStr(pObj, "path", zPath, -1);
|
|
YamlFieldSetInt(pObj, "depth", 0);
|
|
YamlFieldSetStr(pObj, "text", "", 0);
|
|
YamlFieldSetInt(pObj, "intValue", 0);
|
|
YamlFieldSetDouble(pObj, "realValue", 0.0);
|
|
YamlFieldSetBool(pObj, "boolValue", 0);
|
|
YamlFieldSetStr(pObj, "rawNumber", "", 0);
|
|
YamlFieldSetStr(pObj, "tag", "", 0);
|
|
pArray = ph7_context_new_array(pCtx);
|
|
if(pArray == 0 || YamlFieldSetObj(pObj, "children", pArray) != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pObj);
|
|
if(pArray) ph7_context_release_value(pCtx, pArray);
|
|
PH7_VmMemoryError(pVm);
|
|
return 0;
|
|
}
|
|
ph7_context_release_value(pCtx, pArray);
|
|
return pObj;
|
|
}
|
|
/* Recompute eager paths and depths for a node and its whole subtree.
|
|
* The shared blob holds pNode's own path on entry. Each level appends a child
|
|
* suffix and truncates the blob back on return, so no path byte limit and no
|
|
* per-level path buffer are needed. */
|
|
static int YamlRepath(ph7_context *pCtx, SyBlob *pPath, ph7_value *pNode, int nDepth);
|
|
static sxi32 YamlBlobAppendInt(SyBlob *pBlob, int n) {
|
|
char zNum[32];
|
|
SyBufferFormat(zNum, sizeof(zNum), "%d", n);
|
|
return SyBlobAppend(pBlob, zNum, (sxu32)SyStrlen(zNum));
|
|
}
|
|
struct YamlRepathWalk {
|
|
ph7_context *pCtx;
|
|
SyBlob *pPath;
|
|
int nKind;
|
|
int nIndex;
|
|
int nDepth;
|
|
int rc;
|
|
};
|
|
static int YamlRepathWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlRepathWalk *pWalk = (struct YamlRepathWalk *)pUserData;
|
|
sxu32 nMark = SyBlobLength(pWalk->pPath);
|
|
sxi32 rc;
|
|
if(pValue == 0 || (pValue->nType & MEMOBJ_OBJ) == 0 || pValue->x.pOther == 0) {
|
|
return 0;
|
|
}
|
|
if(pWalk->nKind == YAML_NODE_MAPPING) {
|
|
int nKey = 0;
|
|
const char *zKey = YamlKeyDecode(pKey, &nKey);
|
|
rc = SyBlobAppend(pWalk->pPath, ".", 1);
|
|
if(rc == SXRET_OK) {
|
|
rc = SyBlobAppend(pWalk->pPath, zKey ? zKey : "", (sxu32)nKey);
|
|
}
|
|
} else {
|
|
rc = SyBlobAppend(pWalk->pPath, "[", 1);
|
|
if(rc == SXRET_OK) {
|
|
rc = YamlBlobAppendInt(pWalk->pPath, pWalk->nIndex);
|
|
}
|
|
if(rc == SXRET_OK) {
|
|
rc = SyBlobAppend(pWalk->pPath, "]", 1);
|
|
}
|
|
}
|
|
if(rc != SXRET_OK) {
|
|
SyBlobLength(pWalk->pPath) = nMark;
|
|
pWalk->rc = PH7_ABORT;
|
|
return 1;
|
|
}
|
|
++pWalk->nIndex;
|
|
pWalk->rc = YamlRepath(pWalk->pCtx, pWalk->pPath, pValue, pWalk->nDepth + 1);
|
|
SyBlobLength(pWalk->pPath) = nMark;
|
|
return pWalk->rc == PH7_OK ? 0 : 1;
|
|
}
|
|
static int YamlRepath(ph7_context *pCtx, SyBlob *pPath, ph7_value *pNode, int nDepth) {
|
|
ph7_value *pChildren;
|
|
int nKind, rc = PH7_OK;
|
|
/* Depth is enforced by the parser (including alias expansion) and by the
|
|
* DOM mutators; repath only assigns paths/depths and never throws DEPTH. */
|
|
/* YamlFieldSetStr copies before replacing, so a path sourced from this
|
|
* same blob stays valid; the field slot itself is never reused below. */
|
|
(void)YamlFieldSetStr(pNode, "path", (const char *)SyBlobData(pPath), (int)SyBlobLength(pPath));
|
|
(void)YamlFieldSetInt(pNode, "depth", nDepth);
|
|
nKind = (int)YamlFieldInt(pNode, "kind");
|
|
if(nKind != YAML_NODE_MAPPING && nKind != YAML_NODE_SEQUENCE) {
|
|
return PH7_OK;
|
|
}
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
if(pChildren == 0) {
|
|
return PH7_OK;
|
|
}
|
|
{
|
|
struct YamlRepathWalk sWalk;
|
|
sWalk.pCtx = pCtx;
|
|
sWalk.pPath = pPath;
|
|
sWalk.nKind = nKind;
|
|
sWalk.nIndex = 0;
|
|
sWalk.nDepth = nDepth;
|
|
sWalk.rc = PH7_OK;
|
|
(void)ph7_array_walk(pChildren, YamlRepathWalker, &sWalk);
|
|
rc = sWalk.rc;
|
|
}
|
|
return rc;
|
|
}
|
|
/* Start a repath from an explicit path string. */
|
|
static int YamlRepathFrom(ph7_context *pCtx, ph7_value *pNode, const char *zPath, int nDepth) {
|
|
SyBlob sPath;
|
|
int rc;
|
|
if(SyBlobInit(&sPath, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(SyBlobAppend(&sPath, zPath ? zPath : "", (sxu32)SyStrlen(zPath ? zPath : "")) != SXRET_OK) {
|
|
SyBlobRelease(&sPath);
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
rc = YamlRepath(pCtx, &sPath, pNode, nDepth);
|
|
SyBlobRelease(&sPath);
|
|
return rc;
|
|
}
|
|
/* Re-derive paths/depths of a mutated node from its own stored values. */
|
|
static int YamlRepathNode(ph7_context *pCtx, ph7_value *pNode) {
|
|
const char *zSrc = YamlFieldStr(pNode, "path", 0);
|
|
return YamlRepathFrom(pCtx, pNode, zSrc ? zSrc : "$", (int)YamlFieldInt(pNode, "depth"));
|
|
}
|
|
/* Native methods returning a typed array must stamp the value with the same
|
|
* element type the declaration uses: the return checker compares nType. */
|
|
static void YamlStampArrayType(ph7_context *pCtx, ph7_value *pArray, const char *zClass) {
|
|
SyString s;
|
|
sxu32 nType = 0;
|
|
if(zClass == 0) {
|
|
pArray->nType = MEMOBJ_STRING | MEMOBJ_HASHMAP;
|
|
return;
|
|
}
|
|
SyStringInitFromBuf(&s, zClass, (sxu32)SyStrlen(zClass));
|
|
if(PH7_VmInternType(pCtx->pVm, &s, &nType) == SXRET_OK) {
|
|
pArray->nType = nType | MEMOBJ_HASHMAP;
|
|
}
|
|
}
|
|
/* Walk helper that finds an integer-keyed child. The walker hands out a
|
|
* short-lived temporary, so copy into an owned output slot. */
|
|
struct YamlFindInt {
|
|
ph7_int64 iTarget;
|
|
ph7_value *pOut;
|
|
int bFound;
|
|
};
|
|
static int YamlFindIntWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlFindInt *pFind = (struct YamlFindInt *)pUserData;
|
|
if(pKey && ph7_value_is_int(pKey) && ph7_value_to_int64(pKey) == pFind->iTarget) {
|
|
(void)PH7_MemObjStore(pValue, pFind->pOut);
|
|
pFind->bFound = 1;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
static ph7_value *YamlArrayGet(ph7_context *pCtx, ph7_value *pChildren, ph7_int64 iIndex) {
|
|
struct YamlFindInt sFind;
|
|
ph7_value *pOut;
|
|
if(pChildren == 0 || iIndex < 0) {
|
|
return 0;
|
|
}
|
|
pOut = ph7_context_new_scalar(pCtx);
|
|
if(pOut == 0) {
|
|
return 0;
|
|
}
|
|
sFind.iTarget = iIndex;
|
|
sFind.pOut = pOut;
|
|
sFind.bFound = 0;
|
|
(void)ph7_array_walk(pChildren, YamlFindIntWalker, &sFind);
|
|
if(!sFind.bFound) {
|
|
ph7_context_release_value(pCtx, pOut);
|
|
return 0;
|
|
}
|
|
return pOut;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Numeric lexeme helpers */
|
|
/* ------------------------------------------------------------------ */
|
|
/* Decode an int lexeme per YAML 1.2.2 10.3.2: decimal, 0o octal or 0x hex.
|
|
* The base prefix is recognised only at the very start (no sign before it);
|
|
* it has no 0b form. A value that does not fit an int64 (or has a stray digit)
|
|
* is rejected rather than truncated. */
|
|
static int YamlLexemeInt64(const char *z, sxu32 nByte, ph7_int64 *pOut) {
|
|
sxu32 i, k, nBase = 10, nDigit, nDigits = 0;
|
|
sxi32 bNeg = 0;
|
|
sxu64 uVal = 0, uLimit;
|
|
if(nByte < 1 || z == 0) {
|
|
return 0;
|
|
}
|
|
i = 0;
|
|
if(z[0] == '+' || z[0] == '-') {
|
|
bNeg = z[0] == '-';
|
|
++i;
|
|
}
|
|
if(i == 0 && i + 2 <= nByte && z[i] == '0' && (z[i + 1] == 'x' || z[i + 1] == 'X')) {
|
|
nBase = 16;
|
|
i += 2;
|
|
} else if(i == 0 && i + 2 <= nByte && z[i] == '0' && (z[i + 1] == 'o' || z[i + 1] == 'O')) {
|
|
nBase = 8;
|
|
i += 2;
|
|
}
|
|
if(i >= nByte) {
|
|
return 0;
|
|
}
|
|
uLimit = bNeg ? 9223372036854775808ULL : 9223372036854775807ULL;
|
|
for(k = i ; k < nByte ; ++k) {
|
|
unsigned char c = (unsigned char)z[k];
|
|
if(c >= '0' && c <= '9') {
|
|
nDigit = (sxu32)(c - '0');
|
|
} else if(c >= 'a' && c <= 'f') {
|
|
nDigit = (sxu32)(c - 'a') + 10;
|
|
} else if(c >= 'A' && c <= 'F') {
|
|
nDigit = (sxu32)(c - 'A') + 10;
|
|
} else {
|
|
return 0;
|
|
}
|
|
if(nDigit >= nBase || uVal > (uLimit - nDigit) / nBase) {
|
|
return 0;
|
|
}
|
|
uVal = uVal * nBase + nDigit;
|
|
++nDigits;
|
|
}
|
|
if(nDigits < 1) {
|
|
return 0;
|
|
}
|
|
if(bNeg) {
|
|
if(uVal == 9223372036854775808ULL) {
|
|
*pOut = (-9223372036854775807LL - 1);
|
|
} else {
|
|
*pOut = -(ph7_int64)uVal;
|
|
}
|
|
} else {
|
|
*pOut = (ph7_int64)uVal;
|
|
}
|
|
return 1;
|
|
}
|
|
static int YamlFormatReal(char *zBuf, sxu32 nBufSize, ph7_real rVal) {
|
|
char *zEnd;
|
|
sxi32 nPrec, nByte, rc = SXERR_ABORT;
|
|
if(!isfinite((double)rVal)) {
|
|
return SXERR_ABORT;
|
|
}
|
|
#ifdef __WINNT__
|
|
_locale_t pLocale;
|
|
pLocale = _create_locale(LC_NUMERIC, "C");
|
|
if(pLocale == (_locale_t)0) {
|
|
return SXERR_ABORT;
|
|
}
|
|
#else
|
|
locale_t pLocale, pOld;
|
|
pLocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
|
|
if(pLocale == (locale_t)0) {
|
|
return SXERR_ABORT;
|
|
}
|
|
pOld = uselocale(pLocale);
|
|
if(pOld == (locale_t)0) {
|
|
freelocale(pLocale);
|
|
return SXERR_ABORT;
|
|
}
|
|
#endif
|
|
for(nPrec = 15 ; nPrec < 18 ; ++nPrec) {
|
|
#ifdef __WINNT__
|
|
nByte = _snprintf_l(zBuf, (size_t)nBufSize, "%.*g", pLocale, nPrec, (double)rVal);
|
|
#else
|
|
nByte = snprintf(zBuf, (size_t)nBufSize, "%.*g", nPrec, (double)rVal);
|
|
#endif
|
|
if(nByte < 0 || (sxu32)nByte >= nBufSize) {
|
|
break;
|
|
}
|
|
#ifdef __WINNT__
|
|
if(_strtod_l(zBuf, &zEnd, pLocale) == (double)rVal && zEnd[0] == '\0') {
|
|
#else
|
|
if(strtod_l(zBuf, &zEnd, pLocale) == (double)rVal && zEnd[0] == '\0') {
|
|
#endif
|
|
rc = SXRET_OK;
|
|
break;
|
|
}
|
|
}
|
|
#ifdef __WINNT__
|
|
_free_locale(pLocale);
|
|
#else
|
|
if(uselocale(pOld) == (locale_t)0) {
|
|
return SXERR_ABORT;
|
|
}
|
|
freelocale(pLocale);
|
|
#endif
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Mapping-key encoding */
|
|
/* ------------------------------------------------------------------ */
|
|
/* ph7 hashes a string key that looks like a decimal number into an int
|
|
* key (HashmapIsIntKey() even accepts '+1'). Prefixing every internal
|
|
* mapping key with a non-numeric byte forces the blob-key path, so '1',
|
|
* '+1' and '0x1' stay distinct. The prefix is stripped again by every
|
|
* API that exposes or compares the original lexeme; signatures and the
|
|
* text of paths stay unchanged. */
|
|
#define YAML_KEY_PREFIX 0x01
|
|
|
|
static const char *YamlKeyDecode(ph7_value *pKey, int *pLen) {
|
|
int n = 0;
|
|
const char *z = pKey ? ph7_value_to_string(pKey, &n) : 0;
|
|
if(n > 0 && z && (unsigned char)z[0] == YAML_KEY_PREFIX) {
|
|
*pLen = n - 1;
|
|
return z + 1;
|
|
}
|
|
*pLen = n;
|
|
return z ? z : "";
|
|
}
|
|
static ph7_value *YamlMapFetch(ph7_context *pCtx, ph7_value *pChildren,
|
|
const char *zKey, int nKey) {
|
|
char *zEnc;
|
|
ph7_value *pFound;
|
|
if(pChildren == 0 || zKey == 0) {
|
|
return 0;
|
|
}
|
|
zEnc = (char *)SyMemBackendAlloc(&pCtx->pVm->sAllocator, (sxu32)nKey + 1);
|
|
if(zEnc == 0) {
|
|
return 0;
|
|
}
|
|
zEnc[0] = (char)YAML_KEY_PREFIX;
|
|
if(nKey > 0) {
|
|
memcpy(zEnc + 1, zKey, (sxu32)nKey);
|
|
}
|
|
pFound = ph7_array_fetch(pChildren, zEnc, nKey + 1);
|
|
SyMemBackendFree(&pCtx->pVm->sAllocator, zEnc);
|
|
return pFound;
|
|
}
|
|
static int YamlMapStore(ph7_context *pCtx, ph7_value *pChildren,
|
|
const char *zKey, int nKey, ph7_value *pVal) {
|
|
ph7_value *pK = ph7_context_new_scalar(pCtx);
|
|
char *zEnc;
|
|
int rc;
|
|
if(pK == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
zEnc = (char *)SyMemBackendAlloc(&pCtx->pVm->sAllocator, (sxu32)nKey + 1);
|
|
if(zEnc == 0) {
|
|
ph7_context_release_value(pCtx, pK);
|
|
return PH7_ABORT;
|
|
}
|
|
zEnc[0] = (char)YAML_KEY_PREFIX;
|
|
if(nKey > 0) {
|
|
memcpy(zEnc + 1, zKey, (sxu32)nKey);
|
|
}
|
|
rc = ph7_value_string(pK, zEnc, nKey + 1);
|
|
SyMemBackendFree(&pCtx->pVm->sAllocator, zEnc);
|
|
if(rc != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pK);
|
|
return rc;
|
|
}
|
|
rc = ph7_array_add_elem(pChildren, pK, pVal);
|
|
ph7_context_release_value(pCtx, pK);
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Scanner / parser (Phase 2) */
|
|
/* ------------------------------------------------------------------ */
|
|
/* 1-based input position. Mirrors the XML engine's track-position idea
|
|
* but is independent of its token layout: YAML needs an explicit column. */
|
|
typedef struct YamlTrackPos {
|
|
sxu32 nLine; /* 1-based line */
|
|
sxu32 nCol; /* 1-based column (UTF-8 code points) */
|
|
sxu32 nOffset; /* byte offset from the start of the (BOM-stripped) input */
|
|
} YamlTrackPos;
|
|
typedef struct YamlPosMark {
|
|
YamlTrackPos pos;
|
|
int bAfterCR;
|
|
} YamlPosMark;
|
|
|
|
typedef struct YamlProp {
|
|
char *zTag;
|
|
int nTag;
|
|
char *zAnchor;
|
|
int nAnchor;
|
|
int bAnchor;
|
|
int nTagKind; /* 0 = none/unknown, otherwise a YAML_NODE_* kind */
|
|
} YamlProp;
|
|
|
|
typedef struct YamlAnchor {
|
|
char *zName;
|
|
int nName;
|
|
ph7_value *pNode;
|
|
} YamlAnchor;
|
|
|
|
typedef struct YamlTagHandle {
|
|
char *zHandle; /* '!', '!!' or '!name!' */
|
|
int nHandle;
|
|
char *zPrefix; /* URI prefix supplied by %TAG */
|
|
int nPrefix;
|
|
} YamlTagHandle;
|
|
typedef struct yaml_scanner {
|
|
const unsigned char *z; /* input bytes, leading BOM removed */
|
|
char *zOwn; /* owned copy of the input (arena slots can move) */
|
|
sxu32 n; /* byte length */
|
|
YamlTrackPos pos;
|
|
int bAfterCR; /* previous consumed byte was CR */
|
|
yaml_opt *pOpt;
|
|
ph7_context *pCtx;
|
|
int nErr;
|
|
const char *zErrMsg;
|
|
sxu32 nErrLine, nErrCol;
|
|
SyBlob sPath; /* current parse path, one growing buffer */
|
|
char *zErrPath; /* snapshot of sPath at the first error */
|
|
int bPathInit;
|
|
SyBlob sScalar; /* scalar content and flow scalars */
|
|
SyBlob sLines; /* block-scalar raw lines */
|
|
int bScalarInit, bLinesInit;
|
|
YamlAnchor *aAnchor;
|
|
int nAnchor, nAnchorCap;
|
|
YamlTagHandle *aTagHandle; /* %TAG handle -> prefix, per document */
|
|
int nTagHandle, nTagHandleCap;
|
|
int nExpansion;
|
|
} yaml_scanner;
|
|
static const char *YamlTagHandleFind(yaml_scanner *s, const char *zHandle, int nHandle,
|
|
int *pnPrefix);
|
|
|
|
static YamlPosMark YamlMark(yaml_scanner *s) {
|
|
YamlPosMark m;
|
|
m.pos = s->pos;
|
|
m.bAfterCR = s->bAfterCR;
|
|
return m;
|
|
}
|
|
static void YamlRestore(yaml_scanner *s, YamlPosMark m) {
|
|
s->pos = m.pos;
|
|
s->bAfterCR = m.bAfterCR;
|
|
}
|
|
static int YamlAtEnd(yaml_scanner *s) {
|
|
return s->pos.nOffset >= s->n;
|
|
}
|
|
static unsigned char YamlPeekAt(yaml_scanner *s, sxu32 k) {
|
|
sxu32 off = s->pos.nOffset + k;
|
|
if(off >= s->n) {
|
|
return 0;
|
|
}
|
|
return s->z[off];
|
|
}
|
|
/* Consume exactly one byte, folding CR/LF/CRLF into a single line break.
|
|
* NEL/LS/PS are ordinary multi-byte content and never end a line. */
|
|
static unsigned char YamlGet(yaml_scanner *s) {
|
|
unsigned char c;
|
|
if(s->pos.nOffset >= s->n) {
|
|
return 0;
|
|
}
|
|
c = s->z[s->pos.nOffset++];
|
|
if(c == '\r') {
|
|
s->pos.nLine++;
|
|
s->pos.nCol = 1;
|
|
s->bAfterCR = 1;
|
|
} else if(c == '\n') {
|
|
if(s->bAfterCR) {
|
|
s->bAfterCR = 0;
|
|
} else {
|
|
s->pos.nLine++;
|
|
s->pos.nCol = 1;
|
|
}
|
|
} else {
|
|
s->bAfterCR = 0;
|
|
if((c & 0xC0) != 0x80) {
|
|
s->pos.nCol++;
|
|
}
|
|
}
|
|
return c;
|
|
}
|
|
static void YamlSkipNewline(yaml_scanner *s) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
if(c == '\r') {
|
|
YamlGet(s);
|
|
if(YamlPeekAt(s, 0) == '\n') {
|
|
YamlGet(s);
|
|
}
|
|
} else if(c == '\n') {
|
|
YamlGet(s);
|
|
}
|
|
}
|
|
static int YamlIsBreak(unsigned char c) {
|
|
return c == '\n' || c == '\r';
|
|
}
|
|
static int YamlIsSpaceTab(unsigned char c) {
|
|
return c == ' ' || c == '\t';
|
|
}
|
|
/* YAML 1.2.2 6.1: indentation consists of spaces only. TAB is ordinary
|
|
* scalar content and must never be consumed as indentation. */
|
|
static int YamlIsSpace(unsigned char c) {
|
|
return c == ' ';
|
|
}
|
|
static void YamlSkipIndentSpaces(yaml_scanner *s) {
|
|
while(!YamlAtEnd(s) && YamlIsSpace(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
}
|
|
}
|
|
static int YamlIsEolOrEof(yaml_scanner *s) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
return c == 0 || YamlIsBreak(c);
|
|
}
|
|
static void YamlSkipInlineSpace(yaml_scanner *s) {
|
|
while(!YamlAtEnd(s) && YamlIsSpaceTab(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
}
|
|
}
|
|
static void YamlSkipRestOfLine(yaml_scanner *s) {
|
|
while(!YamlAtEnd(s) && !YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
}
|
|
}
|
|
/* Skip empty lines and full-line comments, leaving the cursor just after
|
|
* the leading whitespace of the next content line. */
|
|
static void YamlSkipBlankAndCommentLines(yaml_scanner *s) {
|
|
for(;;) {
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s);
|
|
if(!YamlAtEnd(s) && YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
continue;
|
|
}
|
|
if(!YamlAtEnd(s) && YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
YamlSkipNewline(s);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
#define YAML_PPATH(s) ((const char *)SyBlobData(&(s)->sPath))
|
|
static char *YamlDupBytes(yaml_scanner *s, const char *z, int n);
|
|
static int YamlFail(yaml_scanner *s, int nErr, const char *zMsg) {
|
|
if(s->nErr == YAML_ERROR_NONE) {
|
|
s->nErr = nErr;
|
|
s->zErrMsg = zMsg;
|
|
s->nErrLine = s->pos.nLine;
|
|
s->nErrCol = s->pos.nCol;
|
|
s->zErrPath = s->bPathInit ? YamlDupBytes(s, YAML_PPATH(s),
|
|
(int)SyStrlen(YAML_PPATH(s))) : 0;
|
|
}
|
|
return PH7_ABORT;
|
|
}
|
|
static int YamlFailAt(yaml_scanner *s, int nErr, const char *zMsg,
|
|
sxu32 nLine, sxu32 nCol) {
|
|
if(s->nErr == YAML_ERROR_NONE) {
|
|
s->nErr = nErr;
|
|
s->zErrMsg = zMsg;
|
|
s->nErrLine = nLine;
|
|
s->nErrCol = nCol;
|
|
s->zErrPath = s->bPathInit ? YamlDupBytes(s, YAML_PPATH(s),
|
|
(int)SyStrlen(YAML_PPATH(s))) : 0;
|
|
}
|
|
return PH7_ABORT;
|
|
}
|
|
/* Progress guard. A scanner/parser loop that consumed no byte must fail
|
|
* instead of spinning. The error is always active (production safety); the
|
|
* assertion is compiled only when YAML_DEBUG_PROGRESS is requested via
|
|
* CFLAGS, so the Makefile stays untouched. */
|
|
static int YamlNoProgress(yaml_scanner *s) {
|
|
#ifdef YAML_DEBUG_PROGRESS
|
|
assert(0 && "YAML parser made no progress");
|
|
#endif
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Parser made no progress",
|
|
s->pos.nLine, s->pos.nCol);
|
|
}
|
|
static char *YamlDupBytes(yaml_scanner *s, const char *z, int n) {
|
|
char *p = (char *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator, (sxu32)n + 1);
|
|
if(p) {
|
|
if(n > 0) {
|
|
memcpy(p, z, (sxu32)n);
|
|
}
|
|
p[n] = '\0';
|
|
}
|
|
return p;
|
|
}
|
|
static void YamlPropsFree(yaml_scanner *s, YamlProp *p) {
|
|
SyMemBackend *pA = &s->pCtx->pVm->sAllocator;
|
|
if(p->zTag) {
|
|
SyMemBackendFree(pA, p->zTag);
|
|
}
|
|
if(p->zAnchor) {
|
|
SyMemBackendFree(pA, p->zAnchor);
|
|
}
|
|
SyZero(p, sizeof(*p));
|
|
}
|
|
static int YamlScanInit(yaml_scanner *s, ph7_context *pCtx, const char *zText,
|
|
int nByte, yaml_opt *pOpt) {
|
|
SyZero(s, sizeof(*s));
|
|
s->pCtx = pCtx;
|
|
if(nByte < 0) {
|
|
nByte = (int)SyStrlen(zText);
|
|
}
|
|
/* Own the bytes: ph7_context_new_scalar() may grow the VM arena and move
|
|
* the argument slot that zText points into. */
|
|
s->zOwn = (char *)SyMemBackendAlloc(&pCtx->pVm->sAllocator, (sxu32)nByte + 1);
|
|
if(s->zOwn == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
if(nByte > 0) {
|
|
memcpy(s->zOwn, zText, (sxu32)nByte);
|
|
}
|
|
s->zOwn[nByte] = '\0';
|
|
s->z = (const unsigned char *)s->zOwn;
|
|
s->n = (sxu32)nByte;
|
|
if(s->n >= 3 && s->z[0] == 0xEF && s->z[1] == 0xBB && s->z[2] == 0xBF) {
|
|
s->z += 3;
|
|
s->n -= 3;
|
|
}
|
|
s->pos.nLine = 1;
|
|
s->pos.nCol = 1;
|
|
s->pOpt = pOpt;
|
|
s->pCtx = pCtx;
|
|
if(SyBlobInit(&s->sScalar, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
s->bScalarInit = 1;
|
|
if(SyBlobInit(&s->sLines, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
s->bLinesInit = 1;
|
|
if(SyBlobInit(&s->sPath, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
s->bPathInit = 1;
|
|
SyBlobAppend(&s->sPath, "$\0", 2);
|
|
return PH7_OK;
|
|
}
|
|
/* Path helpers mirror YamlRepathWalker's formatting: '.key' and '[index]'. */
|
|
static void YamlPathRestore(yaml_scanner *s, sxu32 nEnd) {
|
|
SyBlobLength(&s->sPath) = nEnd;
|
|
SyBlobAppend(&s->sPath, "\0", 1);
|
|
}
|
|
static sxu32 YamlPathPush(yaml_scanner *s, int nIndex, const char *zKey, int nKey) {
|
|
sxu32 nMark = SyBlobLength(&s->sPath) - 1;
|
|
int rc;
|
|
SyBlobLength(&s->sPath) = nMark; /* drop the terminating NUL */
|
|
if(nKey >= 0) {
|
|
rc = SyBlobAppend(&s->sPath, ".", 1);
|
|
if(rc == SXRET_OK) rc = SyBlobAppend(&s->sPath, zKey ? zKey : "", (sxu32)nKey);
|
|
} else {
|
|
rc = SyBlobAppend(&s->sPath, "[", 1);
|
|
if(rc == SXRET_OK) rc = YamlBlobAppendInt(&s->sPath, nIndex);
|
|
if(rc == SXRET_OK) rc = SyBlobAppend(&s->sPath, "]", 1);
|
|
}
|
|
if(rc != SXRET_OK) {
|
|
YamlPathRestore(s, nMark);
|
|
return nMark;
|
|
}
|
|
SyBlobAppend(&s->sPath, "\0", 1);
|
|
return nMark;
|
|
}
|
|
/* Drop the %TAG handles declared for one document. */
|
|
static void YamlTagHandlesClear(yaml_scanner *s) {
|
|
int i;
|
|
for(i = 0 ; i < s->nTagHandle ; ++i) {
|
|
if(s->aTagHandle[i].zHandle) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->aTagHandle[i].zHandle);
|
|
}
|
|
if(s->aTagHandle[i].zPrefix) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->aTagHandle[i].zPrefix);
|
|
}
|
|
}
|
|
s->nTagHandle = 0;
|
|
}
|
|
/* Anchors are scoped to one document, like %TAG handles. The capacity is
|
|
* retained for reuse; nExpansion deliberately is NOT reset — the expansion
|
|
* budget counts nodes created by copies across the whole Parse/ParseAll. */
|
|
static void YamlAnchorsClear(yaml_scanner *s) {
|
|
int i;
|
|
for(i = 0 ; i < s->nAnchor ; ++i) {
|
|
if(s->aAnchor[i].zName) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->aAnchor[i].zName);
|
|
}
|
|
}
|
|
s->nAnchor = 0;
|
|
}
|
|
static void YamlScanRelease(yaml_scanner *s) {
|
|
if(s->zErrPath) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->zErrPath);
|
|
}
|
|
if(s->bPathInit) {
|
|
SyBlobRelease(&s->sPath);
|
|
}
|
|
if(s->bScalarInit) {
|
|
SyBlobRelease(&s->sScalar);
|
|
}
|
|
if(s->bLinesInit) {
|
|
SyBlobRelease(&s->sLines);
|
|
}
|
|
YamlAnchorsClear(s);
|
|
if(s->aAnchor) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->aAnchor);
|
|
}
|
|
YamlTagHandlesClear(s);
|
|
if(s->aTagHandle) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->aTagHandle);
|
|
}
|
|
if(s->zOwn) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, s->zOwn);
|
|
}
|
|
}
|
|
/* Validate UTF-8 from the current position; on failure report the position
|
|
* of the first invalid byte. All bytes are consumed through YamlGet so the
|
|
* line/column record stays exact (including CRLF folding). */
|
|
static int YamlValidateEncoding(yaml_scanner *s) {
|
|
while(!YamlAtEnd(s)) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
sxu32 need, k;
|
|
/* NUL (U+0000) is outside YAML's c-printable character set. Reject it
|
|
* during validation: the scanner uses 0 as its end-of-input sentinel,
|
|
* so an embedded 0x00 would stall the cursor in a depth-independent
|
|
* parse recursion at a block-mapping value position. */
|
|
if(c == 0) {
|
|
return YamlFail(s, YAML_ERROR_ENCODING, "NUL byte is not allowed in YAML input");
|
|
}
|
|
if(c < 0x80) {
|
|
YamlGet(s);
|
|
continue;
|
|
}
|
|
if(c >= 0xC2 && c <= 0xDF) {
|
|
need = 1;
|
|
} else if(c >= 0xE0 && c <= 0xEF) {
|
|
need = 2;
|
|
} else if(c >= 0xF0 && c <= 0xF4) {
|
|
need = 3;
|
|
} else {
|
|
return YamlFail(s, YAML_ERROR_ENCODING, "Invalid UTF-8 encoding");
|
|
}
|
|
if(s->pos.nOffset + need >= s->n) {
|
|
return YamlFail(s, YAML_ERROR_ENCODING, "Invalid UTF-8 encoding");
|
|
}
|
|
for(k = 1 ; k <= need ; ++k) {
|
|
if((YamlPeekAt(s, k) & 0xC0) != 0x80) {
|
|
return YamlFail(s, YAML_ERROR_ENCODING, "Invalid UTF-8 encoding");
|
|
}
|
|
}
|
|
if((c == 0xE0 && YamlPeekAt(s, 1) < 0xA0) ||
|
|
(c == 0xED && YamlPeekAt(s, 1) > 0x9F) ||
|
|
(c == 0xF0 && YamlPeekAt(s, 1) < 0x90) ||
|
|
(c == 0xF4 && YamlPeekAt(s, 1) > 0x8F) ||
|
|
/* U+FFFE / U+FFFF are outside c-printable; reject raw bytes
|
|
* just as the \u/\U escape path does. */
|
|
(c == 0xEF && YamlPeekAt(s, 1) == 0xBF &&
|
|
(YamlPeekAt(s, 2) == 0xBE || YamlPeekAt(s, 2) == 0xBF))) {
|
|
return YamlFail(s, YAML_ERROR_ENCODING, "Invalid UTF-8 encoding");
|
|
}
|
|
for(k = 0 ; k <= need ; ++k) {
|
|
YamlGet(s);
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
static ph7_value *YamlAnchorFind(yaml_scanner *s, const char *zName, int nName) {
|
|
int i;
|
|
/* YAML 1.2.2 3.2.2.2: an alias refers to the most recent event carrying
|
|
* the anchor. A name may be reused, so search backwards and return the
|
|
* last earlier definition. */
|
|
for(i = s->nAnchor - 1 ; i >= 0 ; --i) {
|
|
if(s->aAnchor[i].nName == nName &&
|
|
memcmp(s->aAnchor[i].zName, zName, (sxu32)nName) == 0) {
|
|
return s->aAnchor[i].pNode;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
static int YamlAnchorAdd(yaml_scanner *s, const char *zName, int nName,
|
|
ph7_value *pNode) {
|
|
SyMemBackend *pA = &s->pCtx->pVm->sAllocator;
|
|
if(s->nAnchor == s->nAnchorCap) {
|
|
int nCap = s->nAnchorCap ? s->nAnchorCap * 2 : 8;
|
|
YamlAnchor *pNew = (YamlAnchor *)SyMemBackendRealloc(pA, s->aAnchor,
|
|
(sxu32)(nCap * sizeof(YamlAnchor)));
|
|
if(pNew == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
s->aAnchor = pNew;
|
|
s->nAnchorCap = nCap;
|
|
}
|
|
{
|
|
char *zDup = YamlDupBytes(s, zName, nName);
|
|
if(zDup == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
s->aAnchor[s->nAnchor].zName = zDup;
|
|
s->aAnchor[s->nAnchor].nName = nName;
|
|
s->aAnchor[s->nAnchor].pNode = pNode;
|
|
++s->nAnchor;
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* Read an anchor/alias/tag name up to whitespace or a flow indicator. */
|
|
static int YamlReadName(yaml_scanner *s, char **pzOut, int *pnOut) {
|
|
sxu32 nStart = s->pos.nOffset;
|
|
sxu32 n;
|
|
while(!YamlAtEnd(s)) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
if(YamlIsSpaceTab(c) || YamlIsBreak(c) || c == '[' || c == ']' ||
|
|
c == '{' || c == '}' || c == ',') {
|
|
break;
|
|
}
|
|
YamlGet(s);
|
|
}
|
|
n = s->pos.nOffset - nStart;
|
|
if(n < 1) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
*pzOut = YamlDupBytes(s, (const char *)s->z + nStart, (int)n);
|
|
if(*pzOut == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
*pnOut = (int)n;
|
|
return PH7_OK;
|
|
}
|
|
static int YamlStdTagKind(const char *z, int n) {
|
|
static const char *const azName[] = { "str", "int", "float", "bool", "null", "seq", "map" };
|
|
static const int aKind[] = {
|
|
YAML_NODE_STRING, YAML_NODE_INT, YAML_NODE_FLOAT, YAML_NODE_BOOLEAN,
|
|
YAML_NODE_NULL_NODE, YAML_NODE_SEQUENCE, YAML_NODE_MAPPING
|
|
};
|
|
sxu32 i;
|
|
for(i = 0 ; i < SX_ARRAYSIZE(azName) ; ++i) {
|
|
if((int)SyStrlen(azName[i]) == n && memcmp(z, azName[i], (sxu32)n) == 0) {
|
|
return aKind[i];
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
/* ------------------------------------------------------------------ */
|
|
/* Alias expansion: independent deep copies */
|
|
/* ------------------------------------------------------------------ */
|
|
/* An alias is a deep copy of the anchored node, not a shared instance.
|
|
* The copy uses an explicit work queue (no input-dependent recursion).
|
|
* MaxAliasExpansion counts nodes created by copies; the counter is checked
|
|
* before every node creation and the alias position is reported on overflow. */
|
|
typedef struct YamlClonePair {
|
|
ph7_value *pSrc;
|
|
ph7_value *pDst;
|
|
ph7_value *pOwnedSrc; /* snapshot of a short-lived walker temporary */
|
|
} YamlClonePair;
|
|
typedef struct YamlCloneQueue {
|
|
YamlClonePair *a;
|
|
int n, cap;
|
|
} YamlCloneQueue;
|
|
struct YamlCloneWalk {
|
|
yaml_scanner *s;
|
|
YamlCloneQueue *pQ;
|
|
ph7_value *pDstParent;
|
|
sxu32 nLine, nCol;
|
|
int rc;
|
|
};
|
|
static const char *YamlClassForKind(int nKind) {
|
|
switch(nKind) {
|
|
case YAML_NODE_MAPPING: return YCLS_MAPPING;
|
|
case YAML_NODE_SEQUENCE: return YCLS_SEQUENCE;
|
|
case YAML_NODE_STRING: return YCLS_STRING;
|
|
case YAML_NODE_INT: return YCLS_INT;
|
|
case YAML_NODE_FLOAT: return YCLS_FLOAT;
|
|
case YAML_NODE_BOOLEAN: return YCLS_BOOL;
|
|
case YAML_NODE_NULL_NODE: return YCLS_NULL;
|
|
default: return 0;
|
|
}
|
|
}
|
|
static int YamlCloneQueueAdd(yaml_scanner *s, YamlCloneQueue *pQ,
|
|
ph7_value *pSrc, ph7_value *pDst) {
|
|
if(pQ->n == pQ->cap) {
|
|
int nCap = pQ->cap ? pQ->cap * 2 : 16;
|
|
YamlClonePair *pNew = (YamlClonePair *)SyMemBackendRealloc(
|
|
&s->pCtx->pVm->sAllocator, pQ->a, (sxu32)(nCap * sizeof(YamlClonePair)));
|
|
if(pNew == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pQ->a = pNew;
|
|
pQ->cap = nCap;
|
|
}
|
|
pQ->a[pQ->n].pSrc = pSrc;
|
|
pQ->a[pQ->n].pDst = pDst;
|
|
pQ->a[pQ->n].pOwnedSrc = 0;
|
|
++pQ->n;
|
|
return PH7_OK;
|
|
}
|
|
/* ph7_array_walk hands out short-lived temporaries: own the source before
|
|
* queuing it for later processing. */
|
|
static int YamlCloneQueueAddOwned(yaml_scanner *s, YamlCloneQueue *pQ,
|
|
ph7_value *pSrc, ph7_value *pDst) {
|
|
ph7_value *pOwned = ph7_context_new_scalar(s->pCtx);
|
|
int rc;
|
|
if(pOwned == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
PH7_MemObjStore(pSrc, pOwned);
|
|
rc = YamlCloneQueueAdd(s, pQ, pOwned, pDst);
|
|
if(rc != PH7_OK) {
|
|
ph7_context_release_value(s->pCtx, pOwned);
|
|
return rc;
|
|
}
|
|
pQ->a[pQ->n - 1].pOwnedSrc = pOwned;
|
|
return PH7_OK;
|
|
}
|
|
/* Create one node of the copy. Returns 0 with s->nErr set on limit/OOM. */
|
|
static ph7_value *YamlCloneOne(yaml_scanner *s, ph7_value *pSrc,
|
|
sxu32 nLine, sxu32 nCol) {
|
|
ph7_context *pCtx = s->pCtx;
|
|
const char *zClass;
|
|
ph7_value *pDst;
|
|
int n = 0;
|
|
if(++s->nExpansion > s->pOpt->maxAliasExpansion) {
|
|
(void)YamlFailAt(s, YAML_ERROR_ALIAS_LIMIT,
|
|
"Alias expansion limit exceeded", nLine, nCol);
|
|
return 0;
|
|
}
|
|
zClass = YamlClassForKind((int)YamlFieldInt(pSrc, "kind"));
|
|
if(zClass == 0) {
|
|
(void)YamlFailAt(s, YAML_ERROR_TYPE_MISMATCH,
|
|
"Invalid YAML node in alias", nLine, nCol);
|
|
return 0;
|
|
}
|
|
pDst = YamlNewNode(pCtx, zClass, (int)YamlFieldInt(pSrc, "kind"), "$");
|
|
if(pDst == 0) {
|
|
return 0;
|
|
}
|
|
/* Read each length before the setter call: C does not fix argument
|
|
* evaluation order, so inlining YamlFieldStr could read a stale n.
|
|
* YamlNewNode already installs empty/default values, so only write
|
|
* non-default ones (important when a limit allows thousands of copies). */
|
|
{
|
|
const char *zVal = YamlFieldStr(pSrc, "text", &n);
|
|
if(n > 0) YamlFieldSetStr(pDst, "text", zVal, n);
|
|
}
|
|
{
|
|
ph7_int64 iVal = YamlFieldInt(pSrc, "intValue");
|
|
if(iVal != 0) YamlFieldSetInt(pDst, "intValue", iVal);
|
|
}
|
|
{
|
|
double rVal = YamlFieldDouble(pSrc, "realValue");
|
|
if(rVal != 0.0) YamlFieldSetDouble(pDst, "realValue", rVal);
|
|
}
|
|
{
|
|
int bVal = YamlFieldBool(pSrc, "boolValue");
|
|
if(bVal) YamlFieldSetBool(pDst, "boolValue", bVal);
|
|
}
|
|
{
|
|
const char *zVal = YamlFieldStr(pSrc, "rawNumber", &n);
|
|
if(n > 0) YamlFieldSetStr(pDst, "rawNumber", zVal, n);
|
|
}
|
|
{
|
|
const char *zVal = YamlFieldStr(pSrc, "tag", &n);
|
|
if(n > 0) YamlFieldSetStr(pDst, "tag", zVal, n);
|
|
}
|
|
return pDst;
|
|
}
|
|
static int YamlCloneMapWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlCloneWalk *pW = (struct YamlCloneWalk *)pUserData;
|
|
ph7_context *pCtx = pW->s->pCtx;
|
|
const char *zKey;
|
|
int nKey = 0, rc;
|
|
ph7_value *pDstChild, *pChildren;
|
|
zKey = YamlKeyDecode(pKey, &nKey);
|
|
pDstChild = YamlCloneOne(pW->s, pValue, pW->nLine, pW->nCol);
|
|
if(pDstChild == 0) {
|
|
pW->rc = PH7_ABORT;
|
|
return 1;
|
|
}
|
|
rc = YamlCloneQueueAddOwned(pW->s, pW->pQ, pValue, pDstChild);
|
|
if(rc != PH7_OK) {
|
|
pW->rc = rc;
|
|
return 1;
|
|
}
|
|
pChildren = YamlFieldContainer(pW->pDstParent, "children");
|
|
if(pChildren == 0) {
|
|
pW->rc = PH7_CORRUPT;
|
|
return 1;
|
|
}
|
|
rc = YamlMapStore(pCtx, pChildren, zKey, nKey, pDstChild);
|
|
if(rc != PH7_OK) {
|
|
pW->rc = rc;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
static int YamlCloneSeqWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlCloneWalk *pW = (struct YamlCloneWalk *)pUserData;
|
|
ph7_value *pDstChild, *pChildren;
|
|
int rc;
|
|
SXUNUSED(pKey);
|
|
pDstChild = YamlCloneOne(pW->s, pValue, pW->nLine, pW->nCol);
|
|
if(pDstChild == 0) {
|
|
pW->rc = PH7_ABORT;
|
|
return 1;
|
|
}
|
|
rc = YamlCloneQueueAddOwned(pW->s, pW->pQ, pValue, pDstChild);
|
|
if(rc != PH7_OK) {
|
|
pW->rc = rc;
|
|
return 1;
|
|
}
|
|
pChildren = YamlFieldContainer(pW->pDstParent, "children");
|
|
if(pChildren == 0) {
|
|
pW->rc = PH7_CORRUPT;
|
|
return 1;
|
|
}
|
|
ph7_array_add_strkey_elem(pChildren, "", pDstChild);
|
|
return 0;
|
|
}
|
|
/* Bounded iterative walk of a source subtree: counts nodes for the alias
|
|
* expansion budget and measures the subtree height (root level = 1). */
|
|
struct YamlCountFrame {
|
|
yaml_scanner *s;
|
|
int n;
|
|
int nLimit;
|
|
int bOver;
|
|
ph7_value **aOwned;
|
|
int nOwned, capOwned;
|
|
};
|
|
typedef struct YamlCountNode {
|
|
ph7_value *pNode;
|
|
int nLevel;
|
|
} YamlCountNode;
|
|
static int YamlCountChildWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlCountFrame *pF = (struct YamlCountFrame *)pUserData;
|
|
ph7_value *pOwned;
|
|
SXUNUSED(pKey);
|
|
if(!pF->bOver && ++pF->n >= pF->nLimit) {
|
|
pF->bOver = 1;
|
|
}
|
|
if(pF->nOwned == pF->capOwned) {
|
|
int nCap = pF->capOwned ? pF->capOwned * 2 : 16;
|
|
ph7_value **pNew = (ph7_value **)SyMemBackendRealloc(
|
|
&pF->s->pCtx->pVm->sAllocator, pF->aOwned, (sxu32)(nCap * sizeof(ph7_value *)));
|
|
if(pNew == 0) {
|
|
pF->bOver = 1;
|
|
return 1;
|
|
}
|
|
pF->aOwned = pNew;
|
|
pF->capOwned = nCap;
|
|
}
|
|
pOwned = ph7_context_new_scalar(pF->s->pCtx);
|
|
if(pOwned == 0) {
|
|
pF->bOver = 1;
|
|
return 1;
|
|
}
|
|
PH7_MemObjStore(pValue, pOwned);
|
|
pF->aOwned[pF->nOwned++] = pOwned;
|
|
return 0;
|
|
}
|
|
/* Returns 1 when cloning pSrc would exceed the remaining expansion budget;
|
|
* always writes the measured subtree height (root = 1) to pnLevels. */
|
|
static int YamlCloneWouldExceed(yaml_scanner *s, ph7_value *pSrc, int *pnLevels) {
|
|
struct YamlCountFrame f;
|
|
YamlCountNode *aStack = 0;
|
|
int nStack = 0, capStack = 0, i, rc, nMaxLevel = 1;
|
|
SyZero(&f, sizeof(f));
|
|
f.s = s;
|
|
f.n = s->nExpansion;
|
|
f.nLimit = s->pOpt->maxAliasExpansion + 1;
|
|
*pnLevels = 1;
|
|
aStack = (YamlCountNode *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator,
|
|
sizeof(YamlCountNode));
|
|
if(aStack == 0) {
|
|
return 1;
|
|
}
|
|
aStack[nStack].pNode = pSrc; /* root is stable, not owned here */
|
|
aStack[nStack].nLevel = 1;
|
|
nStack++;
|
|
while(nStack > 0) {
|
|
YamlCountNode frame = aStack[--nStack];
|
|
ph7_value *pNode = frame.pNode;
|
|
int nKind, bCollection;
|
|
ph7_value *pChildren;
|
|
if(frame.nLevel > nMaxLevel) {
|
|
nMaxLevel = frame.nLevel;
|
|
}
|
|
nKind = (int)YamlFieldInt(pNode, "kind");
|
|
bCollection = (nKind == YAML_NODE_MAPPING || nKind == YAML_NODE_SEQUENCE);
|
|
pChildren = bCollection ? YamlFieldContainer(pNode, "children") : 0;
|
|
if(pChildren == 0) {
|
|
continue;
|
|
}
|
|
(void)ph7_array_walk(pChildren, YamlCountChildWalker, &f);
|
|
while(f.nOwned > 0) {
|
|
ph7_value *pChild = f.aOwned[--f.nOwned];
|
|
if(nStack == capStack) {
|
|
int nCap = capStack ? capStack * 2 : 16;
|
|
YamlCountNode *pNew = (YamlCountNode *)SyMemBackendRealloc(
|
|
&s->pCtx->pVm->sAllocator, aStack,
|
|
(sxu32)(nCap * sizeof(YamlCountNode)));
|
|
if(pNew == 0) {
|
|
f.bOver = 1;
|
|
break;
|
|
}
|
|
aStack = pNew;
|
|
capStack = nCap;
|
|
}
|
|
aStack[nStack].pNode = pChild;
|
|
aStack[nStack].nLevel = frame.nLevel + 1;
|
|
nStack++;
|
|
}
|
|
}
|
|
*pnLevels = nMaxLevel;
|
|
/* Release any snapshots still owned. */
|
|
for(i = 0 ; i < f.nOwned ; ++i) {
|
|
ph7_context_release_value(s->pCtx, f.aOwned[i]);
|
|
}
|
|
for(i = 0 ; i < nStack ; ++i) {
|
|
if(aStack[i].pNode != pSrc) {
|
|
ph7_context_release_value(s->pCtx, aStack[i].pNode);
|
|
}
|
|
}
|
|
if(f.aOwned) SyMemBackendFree(&s->pCtx->pVm->sAllocator, f.aOwned);
|
|
if(aStack) SyMemBackendFree(&s->pCtx->pVm->sAllocator, aStack);
|
|
rc = f.bOver;
|
|
return rc;
|
|
}
|
|
static ph7_value *YamlCloneNode(yaml_scanner *s, ph7_value *pSrc,
|
|
sxu32 nLine, sxu32 nCol, int nBaseDepth) {
|
|
ph7_context *pCtx = s->pCtx;
|
|
YamlCloneQueue q;
|
|
struct YamlCloneWalk walk;
|
|
ph7_value *pRoot, *pChildren;
|
|
int i, nKind, rc = PH7_OK, nLevels = 1;
|
|
int bOver = YamlCloneWouldExceed(s, pSrc, &nLevels);
|
|
SyZero(&q, sizeof(q));
|
|
/* Depth is checked before the expansion budget and before any copy node
|
|
* is created, so an over-deep alias fails at its own position. */
|
|
if(nBaseDepth + nLevels > s->pOpt->maxDepth) {
|
|
(void)YamlFailAt(s, YAML_ERROR_DEPTH,
|
|
"Maximum depth exceeded", nLine, nCol);
|
|
return 0;
|
|
}
|
|
if(bOver) {
|
|
(void)YamlFailAt(s, YAML_ERROR_ALIAS_LIMIT,
|
|
"Alias expansion limit exceeded", nLine, nCol);
|
|
return 0;
|
|
}
|
|
pRoot = YamlCloneOne(s, pSrc, nLine, nCol);
|
|
if(pRoot == 0) {
|
|
return 0;
|
|
}
|
|
if(YamlCloneQueueAdd(s, &q, pSrc, pRoot) != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pRoot);
|
|
return 0;
|
|
}
|
|
for(i = 0 ; i < q.n ; ++i) {
|
|
nKind = (int)YamlFieldInt(q.a[i].pSrc, "kind");
|
|
if(nKind != YAML_NODE_MAPPING && nKind != YAML_NODE_SEQUENCE) {
|
|
continue;
|
|
}
|
|
pChildren = YamlFieldContainer(q.a[i].pSrc, "children");
|
|
if(pChildren == 0) {
|
|
continue;
|
|
}
|
|
walk.s = s;
|
|
walk.pQ = &q;
|
|
walk.pDstParent = q.a[i].pDst;
|
|
walk.nLine = nLine;
|
|
walk.nCol = nCol;
|
|
walk.rc = PH7_OK;
|
|
(void)ph7_array_walk(pChildren,
|
|
nKind == YAML_NODE_MAPPING ? YamlCloneMapWalker : YamlCloneSeqWalker, &walk);
|
|
rc = walk.rc;
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
}
|
|
/* Context temporaries are no longer needed: each child was snapshotted
|
|
* into its parent's container. Keep the root alive for the caller. */
|
|
for(i = 1 ; i < q.n ; ++i) {
|
|
if(q.a[i].pOwnedSrc) {
|
|
ph7_context_release_value(pCtx, q.a[i].pOwnedSrc);
|
|
}
|
|
ph7_context_release_value(pCtx, q.a[i].pDst);
|
|
}
|
|
if(q.a) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, q.a);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pRoot);
|
|
return 0;
|
|
}
|
|
return pRoot;
|
|
}
|
|
/* Parse leading '!'/'&'/'*' properties. An alias resolves immediately and is
|
|
* returned through ppAlias; other properties are applied to the built node.
|
|
* '?' (explicit key indicator) takes priority and denotes an unsupported
|
|
* complex key. */
|
|
static int YamlParseProperties(yaml_scanner *s, YamlProp *pProp,
|
|
ph7_value **ppAlias, int nDepth) {
|
|
SyZero(pProp, sizeof(*pProp));
|
|
*ppAlias = 0;
|
|
for(;;) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
/* The explicit-key indicator '?' takes priority over a following tag:
|
|
* '? !unknown x' is a complex key, not an unknown-tag node. */
|
|
if(c == '?' && (YamlPeekAt(s, 1) == 0 || YamlIsSpaceTab(YamlPeekAt(s, 1)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 1)))) {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported");
|
|
}
|
|
if(c == '&') {
|
|
if(!s->pOpt->allowAliases) {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFail(s, YAML_ERROR_ALIAS_NOT_ALLOWED, "Aliases are not allowed");
|
|
}
|
|
YamlGet(s);
|
|
if(YamlReadName(s, &pProp->zAnchor, &pProp->nAnchor) != PH7_OK) {
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
pProp->bAnchor = 1;
|
|
} else if(c == '*') {
|
|
ph7_value *pFound;
|
|
sxu32 nAliasLine = s->pos.nLine, nAliasCol = s->pos.nCol;
|
|
if(!s->pOpt->allowAliases) {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFail(s, YAML_ERROR_ALIAS_NOT_ALLOWED, "Aliases are not allowed");
|
|
}
|
|
YamlGet(s);
|
|
{
|
|
char *zName = 0;
|
|
int nName = 0;
|
|
if(YamlReadName(s, &zName, &nName) != PH7_OK) {
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
pFound = YamlAnchorFind(s, zName, nName);
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zName);
|
|
if(pFound == 0) {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFailAt(s, YAML_ERROR_UNDEFINED_ALIAS,
|
|
"Undefined alias", nAliasLine, nAliasCol);
|
|
}
|
|
}
|
|
/* The alias expands to an independent deep copy. Node counting and
|
|
* the limit check happen inside YamlCloneNode (per created node). */
|
|
*ppAlias = YamlCloneNode(s, pFound, nAliasLine, nAliasCol, nDepth);
|
|
YamlPropsFree(s, pProp);
|
|
return *ppAlias ? PH7_OK : PH7_ABORT;
|
|
} else if(c == '!') {
|
|
YamlGet(s);
|
|
if(YamlPeekAt(s, 0) == '<') {
|
|
sxu32 nStart;
|
|
YamlGet(s);
|
|
nStart = s->pos.nOffset;
|
|
while(!YamlAtEnd(s) && YamlPeekAt(s, 0) != '>') {
|
|
YamlGet(s);
|
|
}
|
|
if(YamlPeekAt(s, 0) != '>') {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
pProp->nTag = (int)(s->pos.nOffset - nStart);
|
|
pProp->zTag = YamlDupBytes(s, (const char *)s->z + nStart, pProp->nTag);
|
|
if(pProp->nTag > 18 &&
|
|
SyStrncmp(pProp->zTag, "tag:yaml.org,2002:", 18) == 0) {
|
|
pProp->nTagKind = YamlStdTagKind(pProp->zTag + 18, pProp->nTag - 18);
|
|
}
|
|
YamlGet(s);
|
|
} else if(YamlPeekAt(s, 0) == '!') {
|
|
char *zSuffix = 0;
|
|
int nSuffix = 0, nPrefix = 0;
|
|
const char *zPrefix;
|
|
YamlGet(s);
|
|
if(YamlReadName(s, &zSuffix, &nSuffix) != PH7_OK) {
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
zPrefix = YamlTagHandleFind(s, "!!", 2, &nPrefix);
|
|
if(zPrefix == 0) {
|
|
zPrefix = "tag:yaml.org,2002:";
|
|
nPrefix = (int)SyStrlen(zPrefix);
|
|
}
|
|
pProp->nTag = nPrefix + nSuffix;
|
|
pProp->zTag = (char *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator,
|
|
(sxu32)pProp->nTag + 1);
|
|
if(pProp->zTag == 0) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zSuffix);
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
if(nPrefix > 0) memcpy(pProp->zTag, zPrefix, (sxu32)nPrefix);
|
|
if(nSuffix > 0) memcpy(pProp->zTag + nPrefix, zSuffix, (sxu32)nSuffix);
|
|
pProp->zTag[pProp->nTag] = '\0';
|
|
if(nPrefix == 18 && SyStrncmp(zPrefix, "tag:yaml.org,2002:", 18) == 0) {
|
|
pProp->nTagKind = YamlStdTagKind(zSuffix, nSuffix);
|
|
}
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zSuffix);
|
|
} else {
|
|
char *zName = 0;
|
|
int nName = 0, k, nSuffix, j;
|
|
const char *zPrefix;
|
|
int nPrefix = 0;
|
|
if(YamlReadName(s, &zName, &nName) != PH7_OK) {
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
/* !h!suffix expands through a declared handle; !name without
|
|
* a declaration stays verbatim for compatibility. */
|
|
k = -1;
|
|
for(j = 0 ; j < nName ; ++j) {
|
|
if(zName[j] == '!') {
|
|
k = j;
|
|
break;
|
|
}
|
|
}
|
|
if(k >= 0) {
|
|
char *zHandle;
|
|
nSuffix = nName - k - 1;
|
|
/* The leading '!' was consumed above; rebuild the full
|
|
* handle name ("!e!") for the lookup. */
|
|
zHandle = (char *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator,
|
|
(sxu32)(k + 2) + 1);
|
|
if(zHandle == 0) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zName);
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
zHandle[0] = '!';
|
|
memcpy(zHandle + 1, zName, (sxu32)(k + 1));
|
|
zHandle[k + 2] = '\0';
|
|
zPrefix = YamlTagHandleFind(s, zHandle, k + 2, &nPrefix);
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zHandle);
|
|
} else {
|
|
nSuffix = nName;
|
|
zPrefix = YamlTagHandleFind(s, "!", 1, &nPrefix);
|
|
}
|
|
if(zPrefix) {
|
|
pProp->nTag = nPrefix + nSuffix;
|
|
pProp->zTag = (char *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator,
|
|
(sxu32)pProp->nTag + 1);
|
|
if(pProp->zTag == 0) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zName);
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
if(nPrefix > 0) {
|
|
memcpy(pProp->zTag, zPrefix, (sxu32)nPrefix);
|
|
}
|
|
if(nSuffix > 0) {
|
|
memcpy(pProp->zTag + nPrefix, zName + (k >= 0 ? k + 1 : 0),
|
|
(sxu32)nSuffix);
|
|
}
|
|
pProp->zTag[pProp->nTag] = '\0';
|
|
/* A global tag is never an implicit standard short tag. */
|
|
pProp->nTagKind = 0;
|
|
} else {
|
|
pProp->nTag = nName + 1;
|
|
pProp->zTag = (char *)SyMemBackendAlloc(&s->pCtx->pVm->sAllocator,
|
|
(sxu32)pProp->nTag + 1);
|
|
if(pProp->zTag == 0) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zName);
|
|
YamlPropsFree(s, pProp);
|
|
return PH7_ABORT;
|
|
}
|
|
pProp->zTag[0] = '!';
|
|
if(nName > 0) {
|
|
memcpy(pProp->zTag + 1, zName, (sxu32)nName);
|
|
}
|
|
pProp->zTag[pProp->nTag] = '\0';
|
|
}
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zName);
|
|
}
|
|
if(pProp->nTagKind == 0 && pProp->zTag &&
|
|
s->pOpt->unknownTagPolicy == YAML_UNKNOWN_TAG_REJECT) {
|
|
YamlPropsFree(s, pProp);
|
|
return YamlFail(s, YAML_ERROR_UNKNOWN_TAG, "Unknown tag");
|
|
}
|
|
} else {
|
|
break;
|
|
}
|
|
/* A space is required between consecutive properties. */
|
|
if(!YamlAtEnd(s) && YamlIsSpaceTab(YamlPeekAt(s, 0))) {
|
|
YamlSkipInlineSpace(s);
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int YamlApplyProps(yaml_scanner *s, YamlProp *pProp, ph7_value *pNode) {
|
|
if(pProp->zTag) {
|
|
YamlFieldSetStr(pNode, "tag", pProp->zTag, pProp->nTag);
|
|
}
|
|
if(pProp->bAnchor) {
|
|
if(YamlAnchorAdd(s, pProp->zAnchor, pProp->nAnchor, pNode) != PH7_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* UTF-8 encoder for \x/\u/\U escapes. */
|
|
static int YamlUtf8Put(SyBlob *pBlob, sxu32 cp) {
|
|
char zBuf[4];
|
|
sxu32 n = 0;
|
|
if(cp < 0x80) {
|
|
zBuf[n++] = (char)cp;
|
|
} else if(cp < 0x800) {
|
|
zBuf[n++] = (char)(0xC0 | (cp >> 6));
|
|
zBuf[n++] = (char)(0x80 | (cp & 0x3F));
|
|
} else if(cp < 0x10000) {
|
|
zBuf[n++] = (char)(0xE0 | (cp >> 12));
|
|
zBuf[n++] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
|
zBuf[n++] = (char)(0x80 | (cp & 0x3F));
|
|
} else {
|
|
zBuf[n++] = (char)(0xF0 | (cp >> 18));
|
|
zBuf[n++] = (char)(0x80 | ((cp >> 12) & 0x3F));
|
|
zBuf[n++] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
|
zBuf[n++] = (char)(0x80 | (cp & 0x3F));
|
|
}
|
|
return SyBlobAppend(pBlob, zBuf, n) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
}
|
|
static int YamlHexDigit(unsigned char c) {
|
|
if(c >= '0' && c <= '9') {
|
|
return c - '0';
|
|
}
|
|
if(c >= 'a' && c <= 'f') {
|
|
return c - 'a' + 10;
|
|
}
|
|
if(c >= 'A' && c <= 'F') {
|
|
return c - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
/* A backslash immediately followed by a line break is a continuation: no
|
|
* space is inserted and the next non-empty line's leading white space is
|
|
* consumed. N consecutive empty lines contribute N LF (YAML 1.2.2 7.3.1,
|
|
* verified with PyYAML 6.0.4 BaseLoader). The break itself was already
|
|
* consumed by the caller. */
|
|
static int YamlEscapeBreakContinuation(yaml_scanner *s) {
|
|
sxu32 nEmpty = 0;
|
|
YamlSkipInlineSpace(s);
|
|
while(!YamlAtEnd(s) && YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
++nEmpty;
|
|
YamlSkipNewline(s);
|
|
YamlSkipInlineSpace(s);
|
|
}
|
|
while(nEmpty-- > 0) {
|
|
if(SyBlobAppend(&s->sScalar, "\n", 1) != SXRET_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
/* Parse a backslash escape in a double-quoted scalar. */
|
|
static int YamlParseEscape(yaml_scanner *s) {
|
|
unsigned char c;
|
|
YamlGet(s); /* consume the backslash the caller already saw */
|
|
c = YamlGet(s); /* the escape designator */
|
|
sxu32 cp = 0, k, nHex;
|
|
int d;
|
|
switch(c) {
|
|
case '0': return SyBlobAppend(&s->sScalar, "\0", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'a': return SyBlobAppend(&s->sScalar, "\a", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'b': return SyBlobAppend(&s->sScalar, "\b", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 't': return SyBlobAppend(&s->sScalar, "\t", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'n': return SyBlobAppend(&s->sScalar, "\n", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'v': return SyBlobAppend(&s->sScalar, "\v", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'f': return SyBlobAppend(&s->sScalar, "\f", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'r': return SyBlobAppend(&s->sScalar, "\r", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case 'e': return SyBlobAppend(&s->sScalar, "\x1b", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case ' ': return SyBlobAppend(&s->sScalar, " ", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case '"': return SyBlobAppend(&s->sScalar, "\"", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case '/': return SyBlobAppend(&s->sScalar, "/", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case '\\': return SyBlobAppend(&s->sScalar, "\\", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
/* A backslash at the end of a line is a continuation: consume the
|
|
* break and the next line's leading white space without inserting
|
|
* anything (YAML 1.2.2 7.3.1). */
|
|
case '\r':
|
|
if(YamlPeekAt(s, 0) == '\n') {
|
|
YamlGet(s);
|
|
}
|
|
return YamlEscapeBreakContinuation(s);
|
|
case '\n':
|
|
return YamlEscapeBreakContinuation(s);
|
|
/* An escaped TAB is a literal tab; \_ is the non-breaking space. */
|
|
case '\t': return SyBlobAppend(&s->sScalar, "\t", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
case '_': return YamlUtf8Put(&s->sScalar, 0xA0);
|
|
/* NEL/LS/PS are ordinary content; emit their real UTF-8 bytes in both
|
|
* AllowUnicode modes so \N/\L/\P round-trip. */
|
|
case 'N': return YamlUtf8Put(&s->sScalar, 0x85);
|
|
case 'L': return YamlUtf8Put(&s->sScalar, 0x2028);
|
|
case 'P': return YamlUtf8Put(&s->sScalar, 0x2029);
|
|
case 'x': nHex = 2; break;
|
|
case 'u': nHex = 4; break;
|
|
case 'U': nHex = 8; break;
|
|
default:
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
for(k = 0 ; k < nHex ; ++k) {
|
|
d = YamlHexDigit(YamlPeekAt(s, 0));
|
|
if(d < 0) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
YamlGet(s);
|
|
cp = cp * 16 + (sxu32)d;
|
|
}
|
|
/* \xXX is an 8-bit Unicode character (YAML 1.2.2 5.7), not a raw byte:
|
|
* \xFF must be encoded as U+00FF (C3 BF), never as a lone 0xFF byte. */
|
|
if(cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF) || cp == 0xFFFE || cp == 0xFFFF) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
return YamlUtf8Put(&s->sScalar, cp);
|
|
}
|
|
/* Parse one scalar token into s->sScalar; returns the literal bytes. When
|
|
* bFlow is set, ',', ']' and '}' terminate a plain scalar. nParentIndent >= 0
|
|
* enables multi-line plain scalars: continuation lines must be indented more
|
|
* than the parent node (keys and flow scalars pass -1). */
|
|
static int YamlParseScalarText(yaml_scanner *s, int bFlow, int nParentIndent,
|
|
const char **pz, int *pn, sxu32 *pLine, sxu32 *pCol) {
|
|
unsigned char q;
|
|
SyBlobLength(&s->sScalar) = 0;
|
|
*pLine = s->pos.nLine;
|
|
*pCol = s->pos.nCol;
|
|
q = YamlPeekAt(s, 0);
|
|
if(q == '\'' || q == '"') {
|
|
unsigned char quote = q;
|
|
YamlGet(s);
|
|
for(;;) {
|
|
unsigned char c;
|
|
if(YamlAtEnd(s)) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
c = YamlPeekAt(s, 0);
|
|
if(c == quote) {
|
|
if(quote == '\'' && YamlPeekAt(s, 1) == '\'') {
|
|
SyBlobAppend(&s->sScalar, "'", 1);
|
|
YamlGet(s);
|
|
YamlGet(s);
|
|
continue;
|
|
}
|
|
YamlGet(s);
|
|
break;
|
|
}
|
|
if(c == '\\' && quote == '"') {
|
|
if(YamlParseEscape(s) != PH7_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
continue;
|
|
}
|
|
if(YamlIsBreak(c)) {
|
|
/* YAML 1.2.2 7.3/6.5: trailing white space before the break is
|
|
* trimmed; zero empty lines fold to one space, N empty lines
|
|
* produce N line feeds. */
|
|
sxu32 nLen = SyBlobLength(&s->sScalar);
|
|
while(nLen > 0) {
|
|
char *pData = (char *)SyBlobData(&s->sScalar);
|
|
if(pData[nLen - 1] != ' ' && pData[nLen - 1] != '\t') {
|
|
break;
|
|
}
|
|
SyBlobLength(&s->sScalar) = --nLen;
|
|
}
|
|
YamlSkipNewline(s);
|
|
{
|
|
sxu32 nEmpty = 0;
|
|
for(;;) {
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlAtEnd(s) || !YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
break;
|
|
}
|
|
++nEmpty;
|
|
YamlSkipNewline(s);
|
|
}
|
|
if(nEmpty > 0) {
|
|
while(nEmpty-- > 0) {
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
}
|
|
} else {
|
|
SyBlobAppend(&s->sScalar, " ", 1);
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
YamlGet(s);
|
|
SyBlobAppend(&s->sScalar, &c, 1);
|
|
}
|
|
} else {
|
|
/* plain scalar */
|
|
unsigned char prev = 0;
|
|
while(!YamlAtEnd(s)) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
unsigned char nx = YamlPeekAt(s, 1);
|
|
if(YamlIsBreak(c)) {
|
|
break;
|
|
}
|
|
if(c == ':' && (nx == 0 || YamlIsSpaceTab(nx) || YamlIsBreak(nx))) {
|
|
break;
|
|
}
|
|
if(c == '#' && (prev == 0 || YamlIsSpaceTab(prev))) {
|
|
break;
|
|
}
|
|
if(bFlow && (c == ',' || c == ']' || c == '}')) {
|
|
break;
|
|
}
|
|
YamlGet(s);
|
|
SyBlobAppend(&s->sScalar, &c, 1);
|
|
prev = c;
|
|
}
|
|
/* trim trailing spaces/tabs of a plain scalar */
|
|
while(SyBlobLength(&s->sScalar) > 0) {
|
|
char *pData = (char *)SyBlobData(&s->sScalar);
|
|
sxu32 nLen = SyBlobLength(&s->sScalar);
|
|
if(pData[nLen - 1] != ' ' && pData[nLen - 1] != '\t') {
|
|
break;
|
|
}
|
|
SyBlobLength(&s->sScalar) = nLen - 1;
|
|
}
|
|
/* Multi-line plain scalars (YAML 1.2.2 2.18 / 6.5): a single break
|
|
* folds to a space, N empty lines give N LF. Continuation lines must
|
|
* be indented past the parent node; comments and dedented lines end
|
|
* the scalar and are left for the caller. */
|
|
if(!bFlow && nParentIndent >= 0) {
|
|
for(;;) {
|
|
YamlPosMark save = YamlMark(s);
|
|
sxu32 nEmpty = 0;
|
|
int bStop = 0;
|
|
if(YamlAtEnd(s) || !YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
break;
|
|
}
|
|
YamlSkipNewline(s);
|
|
for(;;) {
|
|
if(YamlAtEnd(s)) {
|
|
bStop = 1;
|
|
break;
|
|
}
|
|
YamlSkipIndentSpaces(s); /* spaces only (6.1) */
|
|
if(YamlAtEnd(s)) {
|
|
bStop = 1;
|
|
break;
|
|
}
|
|
if(YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
++nEmpty;
|
|
YamlSkipNewline(s);
|
|
continue;
|
|
}
|
|
if(YamlPeekAt(s, 0) == '#') {
|
|
bStop = 1;
|
|
break;
|
|
}
|
|
if((int)s->pos.nCol - 1 <= nParentIndent) {
|
|
bStop = 1;
|
|
break;
|
|
}
|
|
/* This line continues the scalar. */
|
|
if(nEmpty > 0) {
|
|
while(nEmpty-- > 0) {
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
}
|
|
} else {
|
|
SyBlobAppend(&s->sScalar, " ", 1);
|
|
}
|
|
{
|
|
unsigned char c2, prev2 = 0;
|
|
while(!YamlAtEnd(s)) {
|
|
c2 = YamlPeekAt(s, 0);
|
|
if(YamlIsBreak(c2)) {
|
|
break;
|
|
}
|
|
if(c2 == ':' && (YamlPeekAt(s, 1) == 0 ||
|
|
YamlIsSpaceTab(YamlPeekAt(s, 1)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 1)))) {
|
|
break;
|
|
}
|
|
if(c2 == '#' && (prev2 == 0 || YamlIsSpaceTab(prev2))) {
|
|
break;
|
|
}
|
|
YamlGet(s);
|
|
SyBlobAppend(&s->sScalar, &c2, 1);
|
|
prev2 = c2;
|
|
}
|
|
while(SyBlobLength(&s->sScalar) > 0) {
|
|
char *pData = (char *)SyBlobData(&s->sScalar);
|
|
sxu32 nLen = SyBlobLength(&s->sScalar);
|
|
if(pData[nLen - 1] != ' ' && pData[nLen - 1] != '\t') {
|
|
break;
|
|
}
|
|
SyBlobLength(&s->sScalar) = nLen - 1;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
if(bStop) {
|
|
YamlRestore(s, save);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*pz = (const char *)SyBlobData(&s->sScalar);
|
|
*pn = (int)SyBlobLength(&s->sScalar);
|
|
if(*pz == 0) {
|
|
*pz = "";
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* Recognize a scalar's implicit type under the active schema. */
|
|
static int YamlLooksInt(const char *z, int n, int nSchema) {
|
|
sxu32 i = 0, nDigits = 0;
|
|
int bHex = 0, bOct = 0;
|
|
if(n < 1) {
|
|
return 0;
|
|
}
|
|
if(z[0] == '+' || z[0] == '-') {
|
|
++i;
|
|
}
|
|
if(i >= (sxu32)n) {
|
|
return 0;
|
|
}
|
|
if(nSchema == YAML_SCHEMA_JSON) {
|
|
if(i > 0 && z[0] == '+') {
|
|
return 0;
|
|
}
|
|
if(i == 0 && n > 1 && z[0] == '0') {
|
|
return 0; /* JSON forbids leading zeros */
|
|
}
|
|
for(; i < (sxu32)n ; ++i) {
|
|
if(z[i] < '0' || z[i] > '9') {
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
if(i == 0 && i + 2 <= (sxu32)n && z[i] == '0') {
|
|
if(z[i + 1] == 'x' || z[i + 1] == 'X') {
|
|
bHex = 1;
|
|
i += 2;
|
|
} else if(z[i + 1] == 'o' || z[i + 1] == 'O') {
|
|
bOct = 1;
|
|
i += 2;
|
|
} else if(n - (int)i > 1) {
|
|
return 0; /* 1.2 core: leading-zero decimals are strings */
|
|
}
|
|
}
|
|
for(; i < (sxu32)n ; ++i) {
|
|
char c = z[i];
|
|
if(bHex) {
|
|
if(!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
|
|
return 0;
|
|
}
|
|
} else if(bOct) {
|
|
if(c < '0' || c > '7') {
|
|
return 0;
|
|
}
|
|
} else {
|
|
if(c < '0' || c > '9') {
|
|
return 0;
|
|
}
|
|
}
|
|
++nDigits;
|
|
}
|
|
return nDigits > 0;
|
|
}
|
|
static int YamlParseFloat(const char *z, int n, double *pOut) {
|
|
char zBuf[128];
|
|
char *zDup = 0;
|
|
char *zEnd = 0;
|
|
double r = 0.0;
|
|
#ifdef __WINNT__
|
|
_locale_t pLocale;
|
|
#else
|
|
locale_t pLocale;
|
|
#endif
|
|
if(n < 1) {
|
|
return 0;
|
|
}
|
|
/* .inf / .nan family (with optional sign). */
|
|
{
|
|
int i = 0;
|
|
int bNeg = 0;
|
|
if(z[0] == '+' || z[0] == '-') {
|
|
bNeg = z[0] == '-';
|
|
i = 1;
|
|
}
|
|
if(n - i == 4 && z[i] == '.' &&
|
|
(SyStrncmp(z + i + 1, "inf", 3) == 0 ||
|
|
SyStrncmp(z + i + 1, "Inf", 3) == 0 ||
|
|
SyStrncmp(z + i + 1, "INF", 3) == 0)) {
|
|
*pOut = bNeg ? -INFINITY : INFINITY;
|
|
return 1;
|
|
}
|
|
if(n - i == 4 && z[i] == '.' &&
|
|
(SyStrncmp(z + i + 1, "nan", 3) == 0 ||
|
|
SyStrncmp(z + i + 1, "NaN", 3) == 0 ||
|
|
SyStrncmp(z + i + 1, "NAN", 3) == 0)) {
|
|
*pOut = NAN;
|
|
return 1;
|
|
}
|
|
}
|
|
{
|
|
int i;
|
|
int bDot = 0, bExp = 0, bDigit = 0;
|
|
for(i = 0 ; i < n ; ++i) {
|
|
char c = z[i];
|
|
if(c >= '0' && c <= '9') {
|
|
bDigit = 1;
|
|
} else if(c == '.') {
|
|
if(bDot || bExp) {
|
|
return 0;
|
|
}
|
|
bDot = 1;
|
|
} else if(c == 'e' || c == 'E') {
|
|
if(bExp || !bDigit) {
|
|
return 0;
|
|
}
|
|
bExp = 1;
|
|
if(i + 1 < n && (z[i + 1] == '+' || z[i + 1] == '-')) {
|
|
++i;
|
|
}
|
|
if(i + 1 >= n) {
|
|
return 0;
|
|
}
|
|
} else if((c == '+' || c == '-') && i == 0) {
|
|
/* sign */
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
if(!bDigit || (!bDot && !bExp)) {
|
|
return 0;
|
|
}
|
|
}
|
|
if(n >= (int)sizeof(zBuf)) {
|
|
return 0; /* unusually long float lexeme: treat as a string */
|
|
}
|
|
memcpy(zBuf, z, (sxu32)n);
|
|
zBuf[n] = '\0';
|
|
zDup = zBuf;
|
|
/* YAML's '.' is always the decimal point, independent of the process
|
|
* locale. _strtod_l/strtod_l with an explicit "C" locale guarantees it. */
|
|
#ifdef __WINNT__
|
|
pLocale = _create_locale(LC_NUMERIC, "C");
|
|
if(pLocale == (_locale_t)0) {
|
|
return 0;
|
|
}
|
|
r = _strtod_l(zDup, &zEnd, pLocale);
|
|
_free_locale(pLocale);
|
|
#else
|
|
{
|
|
locale_t pOld;
|
|
pLocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
|
|
if(pLocale == (locale_t)0) {
|
|
return 0;
|
|
}
|
|
pOld = uselocale(pLocale);
|
|
if(pOld == (locale_t)0) {
|
|
freelocale(pLocale);
|
|
return 0;
|
|
}
|
|
r = strtod_l(zDup, &zEnd, pLocale);
|
|
if(uselocale(pOld) == (locale_t)0) {
|
|
freelocale(pLocale);
|
|
return 0;
|
|
}
|
|
freelocale(pLocale);
|
|
}
|
|
#endif
|
|
if(zEnd != zDup + n || !isfinite(r)) {
|
|
return 0;
|
|
}
|
|
*pOut = r;
|
|
return 1;
|
|
}
|
|
static int YamlResolveKind(yaml_scanner *s, const char *z, int n) {
|
|
double r;
|
|
if(n < 1) {
|
|
return s->pOpt->schema == YAML_SCHEMA_FAILSAFE ? YAML_NODE_STRING : YAML_NODE_NULL_NODE;
|
|
}
|
|
if(s->pOpt->schema == YAML_SCHEMA_FAILSAFE) {
|
|
return YAML_NODE_STRING;
|
|
}
|
|
if((n == 1 && z[0] == '~') ||
|
|
(n == 4 && (SyStrncmp(z, "null", 4) == 0 || SyStrncmp(z, "Null", 4) == 0 ||
|
|
SyStrncmp(z, "NULL", 4) == 0))) {
|
|
return YAML_NODE_NULL_NODE;
|
|
}
|
|
if((n == 4 && (SyStrncmp(z, "true", 4) == 0 || SyStrncmp(z, "True", 4) == 0 ||
|
|
SyStrncmp(z, "TRUE", 4) == 0)) ||
|
|
(n == 5 && (SyStrncmp(z, "false", 5) == 0 || SyStrncmp(z, "False", 5) == 0 ||
|
|
SyStrncmp(z, "FALSE", 5) == 0))) {
|
|
return YAML_NODE_BOOLEAN;
|
|
}
|
|
if(YamlLooksInt(z, n, s->pOpt->schema)) {
|
|
return YAML_NODE_INT;
|
|
}
|
|
if(YamlParseFloat(z, n, &r)) {
|
|
return YAML_NODE_FLOAT;
|
|
}
|
|
return YAML_NODE_STRING;
|
|
}
|
|
|
|
/* Build a typed scalar node from literal bytes and (optional) forced tag. */
|
|
static int YamlMakeScalar(yaml_scanner *s, const char *z, int n, int nTagKind,
|
|
sxu32 nLine, sxu32 nCol, ph7_value **ppOut) {
|
|
ph7_value *pNode = 0;
|
|
ph7_context *pCtx = s->pCtx;
|
|
const char *zNum = z;
|
|
int nNum = n, nKind;
|
|
double rVal = 0.0;
|
|
/* The lexeme is never rewritten: underscores stay part of it, so CORE
|
|
* treats "1_000" as a string (YAML 1.2.2 10.3.2). */
|
|
nKind = nTagKind ? nTagKind : YamlResolveKind(s, zNum, nNum);
|
|
if(nKind == YAML_NODE_SEQUENCE || nKind == YAML_NODE_MAPPING) {
|
|
/* A collection tag cannot turn a scalar into a collection. */
|
|
nKind = YAML_NODE_STRING;
|
|
}
|
|
if(nKind == YAML_NODE_INT) {
|
|
ph7_int64 iVal = 0;
|
|
if(!YamlLexemeInt64(zNum, (sxu32)nNum, &iVal)) {
|
|
return YamlFailAt(s, YAML_ERROR_TYPE_MISMATCH,
|
|
"Cannot represent scalar as an integer", nLine, nCol);
|
|
}
|
|
pNode = YamlNewNode(pCtx, YCLS_INT, YAML_NODE_INT, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
YamlFieldSetInt(pNode, "intValue", iVal);
|
|
YamlFieldSetDouble(pNode, "realValue", (double)iVal);
|
|
YamlFieldSetStr(pNode, "rawNumber", zNum, nNum);
|
|
YamlFieldSetStr(pNode, "text", z, n);
|
|
} else if(nKind == YAML_NODE_FLOAT) {
|
|
if(!YamlParseFloat(zNum, nNum, &rVal)) {
|
|
return YamlFailAt(s, YAML_ERROR_TYPE_MISMATCH,
|
|
"Cannot represent scalar as a float", nLine, nCol);
|
|
}
|
|
pNode = YamlNewNode(pCtx, YCLS_FLOAT, YAML_NODE_FLOAT, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
YamlFieldSetDouble(pNode, "realValue", rVal);
|
|
YamlFieldSetStr(pNode, "rawNumber", zNum, nNum);
|
|
YamlFieldSetStr(pNode, "text", z, n);
|
|
} else if(nKind == YAML_NODE_BOOLEAN) {
|
|
int bVal;
|
|
if(nNum == 4 && (SyStrncmp(zNum, "true", 4) == 0 || SyStrncmp(zNum, "True", 4) == 0 ||
|
|
SyStrncmp(zNum, "TRUE", 4) == 0)) {
|
|
bVal = 1;
|
|
} else if(nNum == 5 && (SyStrncmp(zNum, "false", 5) == 0 ||
|
|
SyStrncmp(zNum, "False", 5) == 0 || SyStrncmp(zNum, "FALSE", 5) == 0)) {
|
|
bVal = 0;
|
|
} else {
|
|
return YamlFailAt(s, YAML_ERROR_TYPE_MISMATCH,
|
|
"Cannot represent scalar as a boolean", nLine, nCol);
|
|
}
|
|
pNode = YamlNewNode(pCtx, YCLS_BOOL, YAML_NODE_BOOLEAN, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
YamlFieldSetBool(pNode, "boolValue", bVal);
|
|
YamlFieldSetStr(pNode, "text", z, n);
|
|
} else if(nKind == YAML_NODE_NULL_NODE) {
|
|
pNode = YamlNewNode(pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
YamlFieldSetStr(pNode, "text", z, n);
|
|
} else {
|
|
pNode = YamlNewNode(pCtx, YCLS_STRING, YAML_NODE_STRING, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
YamlFieldSetStr(pNode, "text", z, n);
|
|
YamlFieldSetStr(pNode, "rawNumber", z, n);
|
|
}
|
|
*ppOut = pNode;
|
|
return PH7_OK;
|
|
}
|
|
|
|
/* Does the current line contain a mapping ':' indicator at top level? */
|
|
static int YamlLineHasKeyIndicator(yaml_scanner *s) {
|
|
sxu32 off = s->pos.nOffset;
|
|
int sq = 0, dq = 0, depth = 0;
|
|
unsigned char prev = 0;
|
|
while(off < s->n) {
|
|
unsigned char c = s->z[off];
|
|
if(YamlIsBreak(c)) {
|
|
break;
|
|
}
|
|
if(dq) {
|
|
if(c == '\\') {
|
|
off += 2;
|
|
continue;
|
|
}
|
|
if(c == '"') {
|
|
dq = 0;
|
|
}
|
|
prev = c;
|
|
++off;
|
|
continue;
|
|
}
|
|
if(sq) {
|
|
if(c == '\'') {
|
|
if(off + 1 < s->n && s->z[off + 1] == '\'') {
|
|
off += 2;
|
|
continue;
|
|
}
|
|
sq = 0;
|
|
}
|
|
prev = c;
|
|
++off;
|
|
continue;
|
|
}
|
|
if(c == '"') {
|
|
dq = 1;
|
|
} else if(c == '\'') {
|
|
sq = 1;
|
|
} else if(c == '[' || c == '{') {
|
|
++depth;
|
|
} else if(c == ']' || c == '}') {
|
|
--depth;
|
|
} else if(c == '#' && (off == s->pos.nOffset || YamlIsSpaceTab(prev))) {
|
|
break;
|
|
} else if(c == ':' && depth <= 0) {
|
|
unsigned char nx = (off + 1 < s->n) ? s->z[off + 1] : 0;
|
|
if(nx == 0 || YamlIsSpaceTab(nx) || YamlIsBreak(nx)) {
|
|
return 1;
|
|
}
|
|
}
|
|
prev = c;
|
|
++off;
|
|
}
|
|
return 0;
|
|
}
|
|
static int YamlIsSeqIndicator(yaml_scanner *s) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
unsigned char nx = YamlPeekAt(s, 1);
|
|
return c == '-' && (nx == 0 || YamlIsSpaceTab(nx) || YamlIsBreak(nx));
|
|
}
|
|
/* '---' / '...' at column 1 delimit documents and end any open block.
|
|
* The character after the marker must be EOF, white space or a line break —
|
|
* exactly the same rule YamlParseOneDoc uses. Otherwise '---a: 1' looks like
|
|
* a marker to the block loops, which then refuse to consume it and spin. */
|
|
static int YamlIsDocMarker(yaml_scanner *s) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
unsigned char nx;
|
|
if(s->pos.nCol != 1) {
|
|
return 0;
|
|
}
|
|
if(c == '-' && YamlPeekAt(s, 1) == '-' && YamlPeekAt(s, 2) == '-') {
|
|
nx = YamlPeekAt(s, 3);
|
|
return nx == 0 || YamlIsSpaceTab(nx) || YamlIsBreak(nx);
|
|
}
|
|
if(c == '.' && YamlPeekAt(s, 1) == '.' && YamlPeekAt(s, 2) == '.') {
|
|
nx = YamlPeekAt(s, 3);
|
|
return nx == 0 || YamlIsSpaceTab(nx) || YamlIsBreak(nx);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int YamlParseDocNode(yaml_scanner *s, int nParentIndent, int nDepth,
|
|
ph7_value **ppOut);
|
|
static int YamlParseBlockSequence(yaml_scanner *s, int nIndent, int nDepth,
|
|
ph7_value **ppOut);
|
|
|
|
/* Block scalar (| / >) with chomping and explicit indentation indicators. */
|
|
static int YamlParseBlockScalar(yaml_scanner *s, int nParentIndent, int nTagKind,
|
|
unsigned char cStyle, ph7_value **ppOut) {
|
|
int bLiteral = (cStyle == '|');
|
|
int nChomp = 0; /* -1 strip, 0 clip, +1 keep */
|
|
int nIndentInd = 0;
|
|
int nContentIndent = -1;
|
|
int rc;
|
|
YamlGet(s); /* consume | or > */
|
|
for(;;) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
if(c == '-') {
|
|
nChomp = -1;
|
|
YamlGet(s);
|
|
} else if(c == '+') {
|
|
nChomp = 1;
|
|
YamlGet(s);
|
|
} else if(c >= '1' && c <= '9') {
|
|
nIndentInd = c - '0';
|
|
YamlGet(s);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
YamlSkipRestOfLine(s);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
SyBlobLength(&s->sLines) = 0;
|
|
{
|
|
int bSeenNonEmpty = 0, nLeadEmptyMax = -1;
|
|
sxu32 nLeadEmptyLine = 0, nLeadEmptyCol = 0;
|
|
for(;;) {
|
|
YamlPosMark lineStart;
|
|
sxu32 k;
|
|
int nLineIndent;
|
|
if(YamlAtEnd(s)) {
|
|
break;
|
|
}
|
|
lineStart = YamlMark(s);
|
|
YamlSkipIndentSpaces(s); /* TAB is content, not indentation */
|
|
if(YamlAtEnd(s) || YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
if(!bSeenNonEmpty) {
|
|
int nEmptyIndent = (int)s->pos.nCol - 1;
|
|
if(nEmptyIndent > nLeadEmptyMax) {
|
|
nLeadEmptyMax = nEmptyIndent;
|
|
nLeadEmptyLine = s->pos.nLine;
|
|
nLeadEmptyCol = s->pos.nCol;
|
|
}
|
|
}
|
|
SyBlobAppend(&s->sLines, "\n", 1);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
continue;
|
|
}
|
|
nLineIndent = (int)s->pos.nCol - 1;
|
|
if(nContentIndent < 0) {
|
|
/* YAML 1.2.2 8.1.1.1: content indentation = the block scalar's own
|
|
* indentation level plus the indicator. nParentIndent is already
|
|
* that level (-1 only for a bare root, whose level is 0). */
|
|
nContentIndent = nIndentInd > 0
|
|
? (nParentIndent < 0 ? 0 : nParentIndent) + nIndentInd : nLineIndent;
|
|
}
|
|
if(!bSeenNonEmpty) {
|
|
/* 8.1.1.1: leading empty lines may not be more indented than the
|
|
* first non-empty line; an explicit indicator may not require
|
|
* more indentation than that line actually has (example 8.3). */
|
|
if(nLeadEmptyMax > nLineIndent) {
|
|
YamlRestore(s, lineStart);
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error",
|
|
nLeadEmptyLine, nLeadEmptyCol);
|
|
}
|
|
if(nIndentInd > 0 && nLineIndent < nContentIndent) {
|
|
YamlRestore(s, lineStart);
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error",
|
|
lineStart.pos.nLine, (sxu32)(nLineIndent + 1));
|
|
}
|
|
bSeenNonEmpty = 1;
|
|
}
|
|
if(nLineIndent < nContentIndent) {
|
|
/* A dedent to the parent level legitimately ends the scalar (e.g.
|
|
* the next mapping key). An in-between indentation cannot start a
|
|
* new block token: reject it instead of silently truncating. */
|
|
int nBase = nParentIndent < 0 ? 0 : nParentIndent;
|
|
YamlRestore(s, lineStart);
|
|
if(bSeenNonEmpty && nLineIndent > nBase) {
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error",
|
|
lineStart.pos.nLine, (sxu32)(nLineIndent + 1));
|
|
}
|
|
break;
|
|
}
|
|
/* Rewind to the line start and strip exactly nContentIndent columns,
|
|
* preserving any extra indentation as literal content. */
|
|
YamlRestore(s, lineStart);
|
|
for(k = 0 ; k < (sxu32)nContentIndent && !YamlAtEnd(s) ; ++k) {
|
|
YamlGet(s);
|
|
}
|
|
while(!YamlAtEnd(s) && !YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
unsigned char c = YamlGet(s);
|
|
SyBlobAppend(&s->sLines, &c, 1);
|
|
}
|
|
SyBlobAppend(&s->sLines, "\n", 1);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
}
|
|
}
|
|
/* Fold into the scalar blob (YAML 1.2.2 6.5, examples 6.6/6.7/8.11):
|
|
* literal style keeps every break; in folded style a run of N empty lines
|
|
* becomes N LF, plus one more LF when either adjacent text line is
|
|
* "more indented" (its surrounding breaks are never folded). Two ordinary
|
|
* non-empty lines fold to one space. nContentIndent columns were stripped
|
|
* on collection, so extra indentation survives as leading whitespace. */
|
|
{
|
|
const char *z = (const char *)SyBlobData(&s->sLines);
|
|
sxu32 n = SyBlobLength(&s->sLines), i = 0;
|
|
SyBlobLength(&s->sScalar) = 0;
|
|
if(bLiteral) {
|
|
while(i < n) {
|
|
sxu32 j = i;
|
|
while(j < n && z[j] != '\n') {
|
|
++j;
|
|
}
|
|
SyBlobAppend(&s->sScalar, z + i, j - i);
|
|
if(j >= n) {
|
|
break;
|
|
}
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
i = j + 1;
|
|
}
|
|
} else {
|
|
/* Leading empty lines are retained verbatim. */
|
|
while(i < n && z[i] == '\n') {
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
++i;
|
|
}
|
|
while(i < n) {
|
|
sxu32 j = i, k, e, nEmpty = 0, q, count;
|
|
int bCurMore, bNextMore = 0;
|
|
while(j < n && z[j] != '\n') {
|
|
++j;
|
|
}
|
|
SyBlobAppend(&s->sScalar, z + i, j - i);
|
|
if(j >= n) {
|
|
break;
|
|
}
|
|
if(j + 1 >= n) {
|
|
/* Final break: leave it for chomping, never fold it. */
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
break;
|
|
}
|
|
bCurMore = j > i && (z[i] == ' ' || z[i] == '\t');
|
|
/* Count the empty lines following this line. */
|
|
q = j + 1;
|
|
while(q < n) {
|
|
e = q;
|
|
while(e < n && z[e] != '\n') {
|
|
++e;
|
|
}
|
|
if(e > q) {
|
|
break;
|
|
}
|
|
++nEmpty;
|
|
q = e + 1;
|
|
}
|
|
if(nEmpty > 0) {
|
|
if(q < n) {
|
|
e = q;
|
|
while(e < n && z[e] != '\n') {
|
|
++e;
|
|
}
|
|
bNextMore = e > q && (z[q] == ' ' || z[q] == '\t');
|
|
}
|
|
count = nEmpty + ((bCurMore || bNextMore) ? 1 : 0);
|
|
while(count-- > 0) {
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
}
|
|
i = q;
|
|
continue;
|
|
}
|
|
k = j + 1;
|
|
e = k;
|
|
while(e < n && z[e] != '\n') {
|
|
++e;
|
|
}
|
|
bNextMore = e > k && (z[k] == ' ' || z[k] == '\t');
|
|
if(bCurMore || bNextMore) {
|
|
SyBlobAppend(&s->sScalar, "\n", 1);
|
|
} else {
|
|
SyBlobAppend(&s->sScalar, " ", 1);
|
|
}
|
|
i = j + 1;
|
|
}
|
|
}
|
|
}
|
|
/* Chomping: trim trailing newlines according to the indicator. */
|
|
{
|
|
sxu32 n = SyBlobLength(&s->sScalar), nEnd = n;
|
|
while(nEnd > 0 && ((char *)SyBlobData(&s->sScalar))[nEnd - 1] == '\n') {
|
|
--nEnd;
|
|
}
|
|
if(nChomp < 0) {
|
|
SyBlobLength(&s->sScalar) = nEnd;
|
|
} else if(nChomp == 0 && n > nEnd) {
|
|
SyBlobLength(&s->sScalar) = nEnd + 1;
|
|
}
|
|
}
|
|
rc = YamlMakeScalar(s, (const char *)SyBlobData(&s->sScalar),
|
|
(int)SyBlobLength(&s->sScalar),
|
|
nTagKind ? nTagKind : YAML_NODE_STRING, 0, 0, ppOut);
|
|
return rc;
|
|
}
|
|
|
|
static int YamlParseFlowValue(yaml_scanner *s, int nDepth, ph7_value **ppOut);
|
|
static int YamlParseFlow(yaml_scanner *s, int nDepth, ph7_value **ppOut);
|
|
static int YamlNewDocument(ph7_context *pCtx, ph7_value **ppDoc);
|
|
|
|
/* Parse an inline node: flow collection, block scalar or quoted/plain scalar.
|
|
* Properties have already been consumed by the caller. */
|
|
static int YamlParseInline(yaml_scanner *s, int nParentIndent, int nDepth,
|
|
YamlProp *pProp, int bFlow, ph7_value **ppOut) {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
int rc;
|
|
if(!bFlow && (c == '|' || c == '>')) {
|
|
return YamlParseBlockScalar(s, nParentIndent, pProp->nTagKind, c, ppOut);
|
|
}
|
|
if(c == '[' || c == '{') {
|
|
return YamlParseFlow(s, nDepth, ppOut);
|
|
}
|
|
{
|
|
const char *z = 0;
|
|
int n = 0;
|
|
sxu32 nLine = 0, nCol = 0;
|
|
rc = YamlParseScalarText(s, bFlow, nParentIndent, &z, &n, &nLine, &nCol);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
return YamlMakeScalar(s, z, n, pProp->nTagKind, nLine, nCol, ppOut);
|
|
}
|
|
}
|
|
static int YamlParseFlowValue(yaml_scanner *s, int nDepth, ph7_value **ppOut) {
|
|
YamlProp prop;
|
|
ph7_value *pAlias = 0, *pNode = 0;
|
|
int rc;
|
|
/* Flow allows newlines/spaces between tokens. */
|
|
for(;;) {
|
|
if(!YamlAtEnd(s) && YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
YamlSkipNewline(s);
|
|
} else if(!YamlAtEnd(s) && YamlIsSpaceTab(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
} else if(!YamlAtEnd(s) && YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
rc = YamlParseProperties(s, &prop, &pAlias, nDepth);
|
|
if(rc != PH7_OK || pAlias) {
|
|
YamlPropsFree(s, &prop);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
*ppOut = pAlias;
|
|
return PH7_OK;
|
|
}
|
|
if(nDepth + 1 > s->pOpt->maxDepth){
|
|
YamlPropsFree(s, &prop);
|
|
return YamlFail(s, YAML_ERROR_DEPTH, "Maximum depth exceeded");
|
|
}
|
|
rc = YamlParseInline(s, 0, nDepth, &prop, 1, &pNode);
|
|
if(rc == PH7_OK) {
|
|
rc = YamlApplyProps(s, &prop, pNode);
|
|
}
|
|
YamlPropsFree(s, &prop);
|
|
if(rc == PH7_OK) {
|
|
*ppOut = pNode;
|
|
}
|
|
return rc;
|
|
}
|
|
/* Forward declaration needed before YamlParseInline uses YamlParseFlow. */
|
|
static int YamlParseFlow(yaml_scanner *s, int nDepth, ph7_value **ppOut) {
|
|
unsigned char c;
|
|
int rc = PH7_OK;
|
|
if(nDepth + 1 > s->pOpt->maxDepth) {
|
|
return YamlFail(s, YAML_ERROR_DEPTH, "Maximum depth exceeded");
|
|
}
|
|
/* Skip leading flow whitespace. */
|
|
for(;;) {
|
|
if(!YamlAtEnd(s) && (YamlIsBreak(YamlPeekAt(s, 0)) || YamlIsSpaceTab(YamlPeekAt(s, 0)))) {
|
|
YamlGet(s);
|
|
} else if(!YamlAtEnd(s) && YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
c = YamlPeekAt(s, 0);
|
|
if(c == '[') {
|
|
ph7_value *pNode, *pChildren;
|
|
YamlGet(s);
|
|
pNode = YamlNewNode(s->pCtx, YCLS_SEQUENCE, YAML_NODE_SEQUENCE, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
{
|
|
sxu32 nPrevOffset = 0, nPathMark = 0; /* checked after the whitespace skip below */
|
|
int nFlowIndex = 0;
|
|
for(;;) {
|
|
ph7_value *pChild = 0;
|
|
while(!YamlAtEnd(s) && (YamlIsBreak(YamlPeekAt(s, 0)) || YamlIsSpaceTab(YamlPeekAt(s, 0)))) {
|
|
YamlGet(s);
|
|
}
|
|
if(!YamlAtEnd(s) && s->pos.nOffset == nPrevOffset) {
|
|
return YamlNoProgress(s);
|
|
}
|
|
nPrevOffset = s->pos.nOffset;
|
|
if(YamlAtEnd(s)) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
if(YamlPeekAt(s, 0) == ']') {
|
|
YamlGet(s);
|
|
break;
|
|
}
|
|
nPathMark = YamlPathPush(s, nFlowIndex++, 0, -1);
|
|
rc = YamlParseFlowValue(s, nDepth + 1, &pChild);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
YamlPathRestore(s, nPathMark);
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
ph7_array_add_strkey_elem(pChildren, "", pChild);
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
while(!YamlAtEnd(s) && (YamlIsBreak(YamlPeekAt(s, 0)) || YamlIsSpaceTab(YamlPeekAt(s, 0)))) {
|
|
YamlGet(s);
|
|
}
|
|
if(YamlPeekAt(s, 0) == ',') {
|
|
YamlGet(s);
|
|
continue;
|
|
}
|
|
if(YamlPeekAt(s, 0) == ']') {
|
|
YamlGet(s);
|
|
break;
|
|
}
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
}
|
|
*ppOut = pNode;
|
|
return PH7_OK;
|
|
}
|
|
if(c == '{') {
|
|
ph7_value *pNode, *pChildren;
|
|
YamlGet(s);
|
|
pNode = YamlNewNode(s->pCtx, YCLS_MAPPING, YAML_NODE_MAPPING, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
{
|
|
sxu32 nPrevOffset = 0; /* checked after the whitespace skip below */
|
|
for(;;) {
|
|
const char *zKey = 0;
|
|
char *zKeyOwn = 0;
|
|
sxu32 nKeyLine = 0, nKeyCol = 0, nPathMark = 0;
|
|
int nKey = 0;
|
|
ph7_value *pChild = 0, *pOld;
|
|
while(!YamlAtEnd(s) && (YamlIsBreak(YamlPeekAt(s, 0)) || YamlIsSpaceTab(YamlPeekAt(s, 0)))) {
|
|
YamlGet(s);
|
|
}
|
|
if(!YamlAtEnd(s) && s->pos.nOffset == nPrevOffset) {
|
|
return YamlNoProgress(s);
|
|
}
|
|
nPrevOffset = s->pos.nOffset;
|
|
if(YamlAtEnd(s)) {
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
if(YamlPeekAt(s, 0) == '}') {
|
|
YamlGet(s);
|
|
break;
|
|
}
|
|
/* '?' is the explicit-key indicator and takes priority over any
|
|
* tag that follows it, here too in flow mappings. */
|
|
if(YamlPeekAt(s, 0) == '?' &&
|
|
(YamlPeekAt(s, 1) == 0 || YamlIsSpaceTab(YamlPeekAt(s, 1)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 1)) || YamlPeekAt(s, 1) == '!' ||
|
|
YamlPeekAt(s, 1) == '[' || YamlPeekAt(s, 1) == '{')) {
|
|
return YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported");
|
|
}
|
|
/* Flow keys are literal scalars: a leading tag would bypass %TAG
|
|
* expansion, so reject it explicitly rather than silently keep it. */
|
|
if(YamlPeekAt(s, 0) == '!') {
|
|
return YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Tags on flow mapping keys are not supported");
|
|
}
|
|
rc = YamlParseScalarText(s, 1, -1, &zKey, &nKey, &nKeyLine, &nKeyCol);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(nKey > 0 && (zKey[0] == '{' || zKey[0] == '[')) {
|
|
return YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported");
|
|
}
|
|
zKeyOwn = YamlDupBytes(s, zKey, nKey);
|
|
if(zKeyOwn == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
zKey = zKeyOwn;
|
|
nPathMark = YamlPathPush(s, 0, zKey, nKey);
|
|
while(!YamlAtEnd(s) && YamlIsSpaceTab(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
}
|
|
if(YamlPeekAt(s, 0) != ':') {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
YamlGet(s);
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
pOld = YamlMapFetch(s->pCtx, pChildren, zKey, nKey);
|
|
if(pOld && s->pOpt->duplicateKeyPolicy == YAML_DUP_REJECT) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
return YamlFailAt(s, YAML_ERROR_DUPLICATE_KEY, "Duplicate key", nKeyLine, nKeyCol);
|
|
}
|
|
rc = YamlParseFlowValue(s, nDepth + 1, &pChild);
|
|
if(rc != PH7_OK) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
return rc;
|
|
}
|
|
YamlPathRestore(s, nPathMark);
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
if(pOld && s->pOpt->duplicateKeyPolicy == YAML_DUP_KEEP_FIRST) {
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
} else {
|
|
rc = YamlMapStore(s->pCtx, pChildren, zKey, nKey, pChild);
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
if(rc != PH7_OK) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
return rc;
|
|
}
|
|
}
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
while(!YamlAtEnd(s) && (YamlIsBreak(YamlPeekAt(s, 0)) || YamlIsSpaceTab(YamlPeekAt(s, 0)))) {
|
|
YamlGet(s);
|
|
}
|
|
if(YamlPeekAt(s, 0) == ',') {
|
|
YamlGet(s);
|
|
continue;
|
|
}
|
|
if(YamlPeekAt(s, 0) == '}') {
|
|
YamlGet(s);
|
|
break;
|
|
}
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
}
|
|
*ppOut = pNode;
|
|
return PH7_OK;
|
|
}
|
|
return YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
}
|
|
|
|
/* The caller has positioned the cursor after a ':' (or '-') and determined
|
|
* that the value starts on following lines. bIndentlessSeq is set for a
|
|
* mapping value: YAML 1.2.2 8.2.1 allows its block sequence to start in the
|
|
* same column as the key (examples 2.3/2.9/2.10). */
|
|
static int YamlParseNestedOrNull(yaml_scanner *s, int nParentIndent, int nDepth,
|
|
int bIndentlessSeq, ph7_value **ppOut) {
|
|
YamlPosMark mark = YamlMark(s);
|
|
int nNextIndent;
|
|
YamlSkipBlankAndCommentLines(s);
|
|
nNextIndent = YamlAtEnd(s) ? -1 : (int)s->pos.nCol - 1;
|
|
if(!YamlAtEnd(s) && nNextIndent == nParentIndent && nParentIndent >= 0 &&
|
|
bIndentlessSeq && YamlIsSeqIndicator(s)) {
|
|
/* A '-' at the key's own column belongs to the value, not to a new
|
|
* sibling token. Parse it as an indentless block sequence. */
|
|
return YamlParseBlockSequence(s, nNextIndent, nDepth, ppOut);
|
|
}
|
|
if(!YamlAtEnd(s) && nNextIndent > nParentIndent) {
|
|
/* Every recursive call must either consume input or increase depth.
|
|
* Skipping the blank/comment lines above advanced the offset; if it
|
|
* did not, fail instead of recursing into the same state. */
|
|
if(s->pos.nOffset <= mark.pos.nOffset) {
|
|
return YamlNoProgress(s);
|
|
}
|
|
return YamlParseDocNode(s, nParentIndent, nDepth, ppOut);
|
|
}
|
|
YamlRestore(s, mark);
|
|
{
|
|
ph7_value *pNode = YamlNewNode(s->pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
*ppOut = pNode;
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
|
|
static int YamlParseBlockMapping(yaml_scanner *s, int nIndent, int nDepth,
|
|
ph7_value **ppOut) {
|
|
ph7_value *pNode, *pChildren;
|
|
if(nDepth + 1 > s->pOpt->maxDepth) {
|
|
return YamlFail(s, YAML_ERROR_DEPTH, "Maximum depth exceeded");
|
|
}
|
|
pNode = YamlNewNode(s->pCtx, YCLS_MAPPING, YAML_NODE_MAPPING, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
{
|
|
sxu32 nIterOffset;
|
|
for(;;) {
|
|
const char *zKey = 0;
|
|
char *zKeyOwn = 0;
|
|
int nKey = 0, rc = PH7_OK;
|
|
sxu32 nKeyLine = 0, nKeyCol = 0, nPathMark = 0;
|
|
ph7_value *pChild = 0, *pOld;
|
|
nIterOffset = s->pos.nOffset;
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(YamlAtEnd(s)) {
|
|
break;
|
|
}
|
|
if((int)s->pos.nCol - 1 != nIndent || YamlIsSeqIndicator(s) || YamlIsDocMarker(s)) {
|
|
break;
|
|
}
|
|
nKeyLine = s->pos.nLine;
|
|
nKeyCol = s->pos.nCol;
|
|
if(YamlPeekAt(s, 0) == '?' && (YamlPeekAt(s, 1) == 0 ||
|
|
YamlIsSpaceTab(YamlPeekAt(s, 1)) || YamlIsBreak(YamlPeekAt(s, 1)))) {
|
|
(void)YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported");
|
|
rc = PH7_ABORT;
|
|
goto DoneEntry;
|
|
}
|
|
rc = YamlParseScalarText(s, 0, -1, &zKey, &nKey, &nKeyLine, &nKeyCol);
|
|
if(rc != PH7_OK) {
|
|
goto DoneEntry;
|
|
}
|
|
if(nKey > 0 && (zKey[0] == '{' || zKey[0] == '[')) {
|
|
(void)YamlFail(s, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported");
|
|
rc = PH7_ABORT;
|
|
goto DoneEntry;
|
|
}
|
|
/* The value parse reuses s->sScalar, so own the key bytes now. */
|
|
zKeyOwn = YamlDupBytes(s, zKey, nKey);
|
|
if(zKeyOwn == 0) {
|
|
rc = PH7_ABORT;
|
|
goto DoneEntry;
|
|
}
|
|
zKey = zKeyOwn;
|
|
nPathMark = YamlPathPush(s, 0, zKey, nKey);
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlPeekAt(s, 0) != ':') {
|
|
(void)YamlFail(s, YAML_ERROR_SYNTAX, "Syntax error");
|
|
rc = PH7_ABORT;
|
|
goto DoneEntry;
|
|
}
|
|
YamlGet(s);
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s); /* trailing comment: value starts later */
|
|
}
|
|
{
|
|
sxu32 nValueLine = s->pos.nLine;
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
pOld = YamlMapFetch(s->pCtx, pChildren, zKey, nKey);
|
|
if(pOld && s->pOpt->duplicateKeyPolicy == YAML_DUP_REJECT) {
|
|
(void)YamlFailAt(s, YAML_ERROR_DUPLICATE_KEY, "Duplicate key", nKeyLine, nKeyCol);
|
|
rc = PH7_ABORT;
|
|
goto DoneEntry;
|
|
}
|
|
if(YamlIsEolOrEof(s)) {
|
|
rc = YamlParseNestedOrNull(s, nIndent, nDepth + 1, 1, &pChild);
|
|
} else {
|
|
rc = YamlParseDocNode(s, nIndent, nDepth + 1, &pChild);
|
|
}
|
|
/* Nested and block-scalar values leave the cursor at the start of
|
|
* the following line; only finish the current line when the value
|
|
* actually ended there. */
|
|
if(rc == PH7_OK && s->pos.nLine == nValueLine) {
|
|
YamlSkipRestOfLine(s);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
}
|
|
}
|
|
if(rc != PH7_OK) {
|
|
goto DoneEntry;
|
|
}
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
if(pOld && s->pOpt->duplicateKeyPolicy == YAML_DUP_KEEP_FIRST) {
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
} else {
|
|
rc = YamlMapStore(s->pCtx, pChildren, zKey, nKey, pChild);
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
if(rc != PH7_OK) {
|
|
goto DoneEntry;
|
|
}
|
|
}
|
|
if(!YamlAtEnd(s) && s->pos.nOffset == nIterOffset) {
|
|
rc = YamlNoProgress(s);
|
|
}
|
|
DoneEntry:
|
|
if(rc == PH7_OK) {
|
|
YamlPathRestore(s, nPathMark);
|
|
}
|
|
if(zKeyOwn) {
|
|
SyMemBackendFree(&s->pCtx->pVm->sAllocator, zKeyOwn);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
}
|
|
*ppOut = pNode;
|
|
return PH7_OK;
|
|
}
|
|
static int YamlParseBlockSequence(yaml_scanner *s, int nIndent, int nDepth,
|
|
ph7_value **ppOut) {
|
|
ph7_value *pNode, *pChildren;
|
|
if(nDepth + 1 > s->pOpt->maxDepth) {
|
|
return YamlFail(s, YAML_ERROR_DEPTH, "Maximum depth exceeded");
|
|
}
|
|
pNode = YamlNewNode(s->pCtx, YCLS_SEQUENCE, YAML_NODE_SEQUENCE, "$");
|
|
if(pNode == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
{
|
|
sxu32 nIterOffset, nPathMark = 0;
|
|
int nSeqIndex = 0;
|
|
for(;;) {
|
|
ph7_value *pChild = 0;
|
|
int rc;
|
|
nIterOffset = s->pos.nOffset;
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(YamlAtEnd(s)) {
|
|
break;
|
|
}
|
|
if((int)s->pos.nCol - 1 != nIndent || !YamlIsSeqIndicator(s) || YamlIsDocMarker(s)) {
|
|
break;
|
|
}
|
|
YamlGet(s); /* consume '-' */
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s);
|
|
}
|
|
{
|
|
sxu32 nValueLine = s->pos.nLine;
|
|
nPathMark = YamlPathPush(s, nSeqIndex++, 0, -1);
|
|
if(YamlIsEolOrEof(s)) {
|
|
rc = YamlParseNestedOrNull(s, nIndent, nDepth + 1, 0, &pChild);
|
|
} else {
|
|
rc = YamlParseDocNode(s, nIndent, nDepth + 1, &pChild);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
YamlPathRestore(s, nPathMark);
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
ph7_array_add_strkey_elem(pChildren, "", pChild);
|
|
/* context owns pChild (aliases share it); do not release here */
|
|
if(s->pos.nLine == nValueLine) {
|
|
YamlSkipRestOfLine(s);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
}
|
|
}
|
|
if(!YamlAtEnd(s) && s->pos.nOffset == nIterOffset) {
|
|
return YamlNoProgress(s);
|
|
}
|
|
}
|
|
}
|
|
*ppOut = pNode;
|
|
return PH7_OK;
|
|
}
|
|
static int YamlParseDocNode(yaml_scanner *s, int nParentIndent, int nDepth,
|
|
ph7_value **ppOut) {
|
|
YamlProp prop;
|
|
ph7_value *pAlias = 0, *pNode = 0;
|
|
int rc;
|
|
rc = YamlParseProperties(s, &prop, &pAlias, nDepth);
|
|
if(rc != PH7_OK || pAlias) {
|
|
YamlPropsFree(s, &prop);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
*ppOut = pAlias;
|
|
return PH7_OK;
|
|
}
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlIsEolOrEof(s)) {
|
|
rc = YamlParseNestedOrNull(s, nParentIndent, nDepth, 0, &pNode);
|
|
} else {
|
|
unsigned char c = YamlPeekAt(s, 0);
|
|
int nIndent = (int)s->pos.nCol - 1;
|
|
if(nDepth + 1 > s->pOpt->maxDepth) {
|
|
rc = YamlFail(s, YAML_ERROR_DEPTH, "Maximum depth exceeded");
|
|
} else if(c == '-' && YamlIsSeqIndicator(s) && nIndent > nParentIndent) {
|
|
rc = YamlParseBlockSequence(s, nIndent, nDepth, &pNode);
|
|
} else if(YamlLineHasKeyIndicator(s) && nIndent > nParentIndent) {
|
|
rc = YamlParseBlockMapping(s, nIndent, nDepth, &pNode);
|
|
} else {
|
|
rc = YamlParseInline(s, nParentIndent, nDepth, &prop, 0, &pNode);
|
|
}
|
|
}
|
|
if(rc == PH7_OK) {
|
|
rc = YamlApplyProps(s, &prop, pNode);
|
|
}
|
|
YamlPropsFree(s, &prop);
|
|
if(rc == PH7_OK) {
|
|
*ppOut = pNode;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/* Locate a declared %TAG handle. Returns the prefix (possibly empty) or 0. */
|
|
static const char *YamlTagHandleFind(yaml_scanner *s, const char *zHandle, int nHandle,
|
|
int *pnPrefix) {
|
|
int i;
|
|
for(i = 0 ; i < s->nTagHandle ; ++i) {
|
|
if(s->aTagHandle[i].nHandle == nHandle &&
|
|
memcmp(s->aTagHandle[i].zHandle, zHandle, (sxu32)nHandle) == 0) {
|
|
*pnPrefix = s->aTagHandle[i].nPrefix;
|
|
return s->aTagHandle[i].zPrefix;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
/* Parse a %TAG directive line. The cursor is just after '%TAG'. */
|
|
static int YamlParseTagDirective(yaml_scanner *s) {
|
|
ph7_vm *pVm = s->pCtx->pVm;
|
|
sxu32 nLine = s->pos.nLine, nCol = s->pos.nCol;
|
|
char *zHandle = 0, *zPrefix = 0;
|
|
int nHandle = 0, nPrefix = 0, i, rc;
|
|
sxu32 nStart;
|
|
YamlSkipInlineSpace(s);
|
|
nStart = s->pos.nOffset;
|
|
while(!YamlAtEnd(s) && !YamlIsSpaceTab(YamlPeekAt(s, 0)) &&
|
|
!YamlIsBreak(YamlPeekAt(s, 0))) {
|
|
YamlGet(s);
|
|
}
|
|
nHandle = (int)(s->pos.nOffset - nStart);
|
|
if(nHandle < 1 || s->z[nStart] != '!') {
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error", nLine, nCol);
|
|
}
|
|
zHandle = YamlDupBytes(s, (const char *)s->z + nStart, nHandle);
|
|
if(zHandle == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
/* Valid handles: '!' (primary), '!!' (secondary) or '!name!'. */
|
|
if(nHandle != 1 && !(nHandle == 2 && zHandle[1] == '!') &&
|
|
!(nHandle > 2 && zHandle[nHandle - 1] == '!')) {
|
|
SyMemBackendFree(&pVm->sAllocator, zHandle);
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error", nLine, nCol);
|
|
}
|
|
YamlSkipInlineSpace(s);
|
|
nStart = s->pos.nOffset;
|
|
while(!YamlAtEnd(s) && !YamlIsSpaceTab(YamlPeekAt(s, 0)) &&
|
|
!YamlIsBreak(YamlPeekAt(s, 0)) && YamlPeekAt(s, 0) != '#') {
|
|
YamlGet(s);
|
|
}
|
|
nPrefix = (int)(s->pos.nOffset - nStart);
|
|
if(nPrefix < 1) {
|
|
SyMemBackendFree(&pVm->sAllocator, zHandle);
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Syntax error", nLine, nCol);
|
|
}
|
|
zPrefix = YamlDupBytes(s, (const char *)s->z + nStart, nPrefix);
|
|
if(zPrefix == 0) {
|
|
SyMemBackendFree(&pVm->sAllocator, zHandle);
|
|
return PH7_ABORT;
|
|
}
|
|
if(YamlTagHandleFind(s, zHandle, nHandle, &i) != 0) {
|
|
SyMemBackendFree(&pVm->sAllocator, zHandle);
|
|
SyMemBackendFree(&pVm->sAllocator, zPrefix);
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX, "Duplicate TAG handle", nLine, nCol);
|
|
}
|
|
if(s->nTagHandle == s->nTagHandleCap) {
|
|
int nCap = s->nTagHandleCap ? s->nTagHandleCap * 2 : 4;
|
|
YamlTagHandle *pNew = (YamlTagHandle *)SyMemBackendRealloc(
|
|
&pVm->sAllocator, s->aTagHandle, (sxu32)(nCap * sizeof(YamlTagHandle)));
|
|
if(pNew == 0) {
|
|
SyMemBackendFree(&pVm->sAllocator, zHandle);
|
|
SyMemBackendFree(&pVm->sAllocator, zPrefix);
|
|
return PH7_ABORT;
|
|
}
|
|
s->aTagHandle = pNew;
|
|
s->nTagHandleCap = nCap;
|
|
}
|
|
s->aTagHandle[s->nTagHandle].zHandle = zHandle;
|
|
s->aTagHandle[s->nTagHandle].nHandle = nHandle;
|
|
s->aTagHandle[s->nTagHandle].zPrefix = zPrefix;
|
|
s->aTagHandle[s->nTagHandle].nPrefix = nPrefix;
|
|
++s->nTagHandle;
|
|
rc = PH7_OK;
|
|
return rc;
|
|
}
|
|
static int YamlParseOneDoc(yaml_scanner *s, ph7_value **ppRoot) {
|
|
ph7_value *pRoot = 0;
|
|
int rc;
|
|
/* Anchors and %TAG handles are scoped to one document; nExpansion stays
|
|
* cumulative over the whole Parse/ParseAll call. */
|
|
YamlAnchorsClear(s);
|
|
YamlTagHandlesClear(s);
|
|
for(;;) {
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(s->pos.nCol == 1 && YamlPeekAt(s, 0) == '%') {
|
|
sxu32 nDirLine = s->pos.nLine, nDirCol = s->pos.nCol;
|
|
YamlGet(s); /* consume '%' */
|
|
if(s->pos.nOffset + 3 <= s->n &&
|
|
SyStrncmp((const char *)s->z + s->pos.nOffset, "TAG", 3) == 0 &&
|
|
(YamlIsSpaceTab(YamlPeekAt(s, 3)) || YamlIsBreak(YamlPeekAt(s, 3)) ||
|
|
YamlPeekAt(s, 3) == 0)) {
|
|
YamlGet(s); YamlGet(s); YamlGet(s); /* 'TAG' */
|
|
rc = YamlParseTagDirective(s);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
} else if(s->pos.nOffset + 4 <= s->n &&
|
|
SyStrncmp((const char *)s->z + s->pos.nOffset, "YAML", 4) == 0 &&
|
|
(YamlIsSpaceTab(YamlPeekAt(s, 4)) || YamlIsBreak(YamlPeekAt(s, 4)) ||
|
|
YamlPeekAt(s, 4) == 0)) {
|
|
int bVer;
|
|
YamlGet(s); YamlGet(s); YamlGet(s); YamlGet(s); /* 'YAML' */
|
|
YamlSkipInlineSpace(s);
|
|
bVer = YamlPeekAt(s, 0) >= '0' && YamlPeekAt(s, 0) <= '9' &&
|
|
YamlPeekAt(s, 1) == '.' && YamlPeekAt(s, 2) >= '0' &&
|
|
YamlPeekAt(s, 2) <= '9' &&
|
|
(YamlPeekAt(s, 3) == 0 || YamlIsSpaceTab(YamlPeekAt(s, 3)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 3)) || YamlPeekAt(s, 3) == '#');
|
|
if(!bVer) {
|
|
return YamlFailAt(s, YAML_ERROR_SYNTAX,
|
|
"Invalid YAML directive version", nDirLine, nDirCol);
|
|
}
|
|
/* The version is accepted but has no effect on the DOM. */
|
|
}
|
|
/* Unknown directives are ignored per YAML 1.2.2 6.8. */
|
|
YamlSkipRestOfLine(s);
|
|
if(!YamlAtEnd(s)) {
|
|
YamlSkipNewline(s);
|
|
}
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
if(s->pos.nCol == 1 && YamlPeekAt(s, 0) == '-' && YamlPeekAt(s, 1) == '-' &&
|
|
YamlPeekAt(s, 2) == '-' &&
|
|
(YamlPeekAt(s, 3) == 0 || YamlIsSpaceTab(YamlPeekAt(s, 3)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 3)))) {
|
|
YamlGet(s);
|
|
YamlGet(s);
|
|
YamlGet(s);
|
|
YamlSkipInlineSpace(s);
|
|
if(YamlPeekAt(s, 0) == '#') {
|
|
YamlSkipRestOfLine(s);
|
|
}
|
|
}
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(YamlAtEnd(s) || YamlIsEolOrEof(s)) {
|
|
pRoot = YamlNewNode(s->pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
if(pRoot == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
} else {
|
|
rc = YamlParseDocNode(s, -1, 0, &pRoot);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
*ppRoot = pRoot;
|
|
return PH7_OK;
|
|
}
|
|
static void YamlFinishDoc(yaml_scanner *s) {
|
|
if(!YamlIsDocMarker(s)) {
|
|
YamlSkipRestOfLine(s);
|
|
}
|
|
for(;;) {
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(s->pos.nCol == 1 && YamlPeekAt(s, 0) == '.' && YamlPeekAt(s, 1) == '.' &&
|
|
YamlPeekAt(s, 2) == '.' &&
|
|
(YamlPeekAt(s, 3) == 0 || YamlIsSpaceTab(YamlPeekAt(s, 3)) ||
|
|
YamlIsBreak(YamlPeekAt(s, 3)))) {
|
|
YamlGet(s);
|
|
YamlGet(s);
|
|
YamlGet(s);
|
|
YamlSkipRestOfLine(s);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
static int YamlCheckSingleDoc(yaml_scanner *s) {
|
|
YamlFinishDoc(s);
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(YamlAtEnd(s)) {
|
|
return PH7_OK;
|
|
}
|
|
return YamlFailAt(s, YAML_ERROR_INVALID_OPERATION,
|
|
"Stream contains more than one document; use Yaml::ParseAll()",
|
|
s->pos.nLine, s->pos.nCol);
|
|
}
|
|
|
|
/* Wrap a parsed root in a YamlDocument, assigning eager paths/depths first. */
|
|
static int YamlDocFromRoot(ph7_context *pCtx, ph7_value *pRoot, ph7_value **ppDoc) {
|
|
int rc = YamlRepathFrom(pCtx, pRoot, "$", 0);
|
|
ph7_value *pDoc = 0;
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = YamlNewDocument(pCtx, &pDoc); /* creates a null root we overwrite */
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
(void)YamlFieldSetObj(pDoc, "root", pRoot);
|
|
*ppDoc = pDoc;
|
|
return PH7_OK;
|
|
}
|
|
/* Parse the stream according to bAll and translate a scanner error into a
|
|
* real YamlException with the recorded 1-based input position. */
|
|
static int YamlRunParse(yaml_scanner *s, int bAll, ph7_value **ppFirstRoot,
|
|
ph7_value **ppDocs) {
|
|
YamlPosMark mark = YamlMark(s);
|
|
int rc = YamlValidateEncoding(s);
|
|
if(rc != PH7_OK) {
|
|
goto Done;
|
|
}
|
|
YamlRestore(s, mark); /* validation consumed the stream; rewind it */
|
|
if(bAll) {
|
|
ph7_value *pDocs = ph7_context_new_array(s->pCtx);
|
|
if(pDocs == 0) {
|
|
rc = PH7_ABORT;
|
|
goto Done;
|
|
}
|
|
for(;;) {
|
|
ph7_value *pRoot = 0, *pDoc = 0;
|
|
YamlSkipBlankAndCommentLines(s);
|
|
if(YamlAtEnd(s)) {
|
|
break;
|
|
}
|
|
rc = YamlParseOneDoc(s, &pRoot);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
rc = YamlDocFromRoot(s->pCtx, pRoot, &pDoc);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
rc = ph7_array_add_strkey_elem(pDocs, "", pDoc);
|
|
ph7_context_release_value(s->pCtx, pDoc);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
/* Guarantee forward progress: finishing a document must consume
|
|
* input unless the stream actually ended. */
|
|
mark = YamlMark(s);
|
|
YamlFinishDoc(s);
|
|
/* Sitting exactly at the next '---' is a legitimate document end:
|
|
* FinishDoc correctly consumes nothing there. */
|
|
if(s->pos.nOffset == mark.pos.nOffset && !YamlAtEnd(s) &&
|
|
!YamlIsDocMarker(s)) {
|
|
rc = YamlNoProgress(s);
|
|
break;
|
|
}
|
|
}
|
|
if(rc == PH7_OK) {
|
|
/* An empty stream is represented as one empty document. */
|
|
if(ph7_array_count(pDocs) == 0) {
|
|
ph7_value *pRoot = YamlNewNode(s->pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
ph7_value *pDoc = 0;
|
|
if(pRoot == 0) {
|
|
rc = PH7_ABORT;
|
|
} else {
|
|
rc = YamlDocFromRoot(s->pCtx, pRoot, &pDoc);
|
|
if(rc == PH7_OK) {
|
|
ph7_array_add_strkey_elem(pDocs, "", pDoc);
|
|
ph7_context_release_value(s->pCtx, pDoc);
|
|
}
|
|
}
|
|
}
|
|
if(rc == PH7_OK) {
|
|
YamlStampArrayType(s->pCtx, pDocs, YCLS_DOC);
|
|
*ppDocs = pDocs;
|
|
}
|
|
}
|
|
if(rc != PH7_OK) {
|
|
ph7_context_release_value(s->pCtx, pDocs);
|
|
}
|
|
} else {
|
|
rc = YamlParseOneDoc(s, ppFirstRoot);
|
|
if(rc == PH7_OK) {
|
|
rc = YamlCheckSingleDoc(s);
|
|
}
|
|
}
|
|
Done:
|
|
if(rc != PH7_OK && s->nErr != YAML_ERROR_NONE) {
|
|
rc = YamlThrow(s->pCtx, s->nErr, s->zErrMsg, s->nErrLine, s->nErrCol,
|
|
s->zErrPath ? s->zErrPath : "$");
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Serializer (Phase 3) */
|
|
/* ------------------------------------------------------------------ */
|
|
/* Canonical, deterministic emitter. The DOM does not retain the source
|
|
* flow/block style or comments, so block style with the indentation from
|
|
* YamlOptions is always used. Parsed aliases are independent deep copies, so
|
|
* no sharing workaround is needed here; YamlEmitPush only guards against
|
|
* cycles introduced through the DOM mutation API. */
|
|
|
|
typedef struct yaml_emit {
|
|
ph7_context *pCtx;
|
|
yaml_opt *pOpt;
|
|
SyBlob sOut;
|
|
int bOutInit;
|
|
const char *zPath; /* path of the node currently being emitted */
|
|
ph7_class_instance **aActive; /* nodes on the current recursion path */
|
|
int nActive, nActiveCap;
|
|
} yaml_emit;
|
|
|
|
static int YamlEmitBytes(yaml_emit *pE, const char *z, int n) {
|
|
if(n < 0) {
|
|
n = (int)SyStrlen(z);
|
|
}
|
|
return SyBlobAppend(&pE->sOut, z, (sxu32)n) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
}
|
|
static int YamlEmitCh(yaml_emit *pE, char c) {
|
|
return YamlEmitBytes(pE, &c, 1);
|
|
}
|
|
static void YamlEmitRelease(yaml_emit *pE) {
|
|
if(pE->bOutInit) {
|
|
SyBlobRelease(&pE->sOut);
|
|
}
|
|
if(pE->aActive) {
|
|
SyMemBackendFree(&pE->pCtx->pVm->sAllocator, pE->aActive);
|
|
}
|
|
}
|
|
static int YamlEmitInit(yaml_emit *pE, ph7_context *pCtx, yaml_opt *pOpt) {
|
|
SyZero(pE, sizeof(*pE));
|
|
pE->pCtx = pCtx;
|
|
pE->pOpt = pOpt;
|
|
pE->zPath = "$";
|
|
if(SyBlobInit(&pE->sOut, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
return PH7_ABORT;
|
|
}
|
|
pE->bOutInit = 1;
|
|
return PH7_OK;
|
|
}
|
|
/* 1 = pushed, 0 = cycle, -1 = allocation failure. */
|
|
static int YamlEmitPush(yaml_emit *pE, ph7_value *pNode) {
|
|
ph7_class_instance *pInst = (ph7_class_instance *)pNode->x.pOther;
|
|
int i;
|
|
for(i = 0 ; i < pE->nActive ; ++i) {
|
|
if(pE->aActive[i] == pInst) {
|
|
return 0;
|
|
}
|
|
}
|
|
if(pE->nActive == pE->nActiveCap) {
|
|
int nCap = pE->nActiveCap ? pE->nActiveCap * 2 : 16;
|
|
ph7_class_instance **pNew = (ph7_class_instance **)SyMemBackendRealloc(
|
|
&pE->pCtx->pVm->sAllocator, pE->aActive,
|
|
(sxu32)(nCap * sizeof(ph7_class_instance *)));
|
|
if(pNew == 0) {
|
|
return -1;
|
|
}
|
|
pE->aActive = pNew;
|
|
pE->nActiveCap = nCap;
|
|
}
|
|
pE->aActive[pE->nActive++] = pInst;
|
|
return 1;
|
|
}
|
|
static void YamlEmitPop(yaml_emit *pE) {
|
|
if(pE->nActive > 0) {
|
|
--pE->nActive;
|
|
}
|
|
}
|
|
static int YamlEmitIndent(yaml_emit *pE, int nDepth) {
|
|
int i, k, rc;
|
|
for(k = 0 ; k < nDepth ; ++k) {
|
|
for(i = 0 ; i < pE->pOpt->indentWidth ; ++i) {
|
|
rc = YamlEmitCh(pE, ' ');
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
/* Decode one UTF-8 code point. Rejects surrogates, overlong forms and the
|
|
* noncharacters U+FFFE/U+FFFF, so the emitter never produces them. */
|
|
static int YamlUtf8Next(const unsigned char *z, int n, int *pi, sxu32 *pcp) {
|
|
unsigned char c = z[*pi];
|
|
sxu32 cp;
|
|
int need, k;
|
|
if(c < 0x80) {
|
|
*pcp = c;
|
|
++(*pi);
|
|
return 1;
|
|
}
|
|
if(c >= 0xC2 && c <= 0xDF) {
|
|
need = 1; cp = c & 0x1F;
|
|
} else if(c >= 0xE0 && c <= 0xEF) {
|
|
need = 2; cp = c & 0x0F;
|
|
} else if(c >= 0xF0 && c <= 0xF4) {
|
|
need = 3; cp = c & 0x07;
|
|
} else {
|
|
return 0;
|
|
}
|
|
if(*pi + need >= n) {
|
|
return 0;
|
|
}
|
|
for(k = 1 ; k <= need ; ++k) {
|
|
unsigned char d = z[*pi + k];
|
|
if((d & 0xC0) != 0x80) {
|
|
return 0;
|
|
}
|
|
cp = (cp << 6) | (d & 0x3F);
|
|
}
|
|
if((need == 2 && cp < 0x800) || (need == 3 && cp < 0x10000)) {
|
|
return 0;
|
|
}
|
|
if(cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF) || cp == 0xFFFE || cp == 0xFFFF) {
|
|
return 0;
|
|
}
|
|
*pi += need + 1;
|
|
*pcp = cp;
|
|
return 1;
|
|
}
|
|
static int YamlEmitHexEscape(yaml_emit *pE, char cPrefix, sxu32 cp, int nDigits) {
|
|
static const char azHex[] = "0123456789ABCDEF";
|
|
char zBuf[12];
|
|
int i, n = 0;
|
|
zBuf[n++] = '\\';
|
|
zBuf[n++] = cPrefix;
|
|
for(i = nDigits - 1 ; i >= 0 ; --i) {
|
|
zBuf[n++] = azHex[(cp >> (i * 4)) & 0xF];
|
|
}
|
|
return YamlEmitBytes(pE, zBuf, n);
|
|
}
|
|
static int YamlEmitDoubleQuoted(yaml_emit *pE, const char *z, int n) {
|
|
const unsigned char *p = (const unsigned char *)z;
|
|
int i = 0, rc;
|
|
rc = YamlEmitCh(pE, '"');
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
while(i < n) {
|
|
unsigned char c = p[i];
|
|
sxu32 cp;
|
|
int nStart;
|
|
if(c == '"' || c == '\\') {
|
|
char zSeq[2];
|
|
zSeq[0] = '\\';
|
|
zSeq[1] = (char)c;
|
|
rc = YamlEmitBytes(pE, zSeq, 2);
|
|
++i;
|
|
} else if(c == '\0') {
|
|
rc = YamlEmitBytes(pE, "\\0", 2); ++i;
|
|
} else if(c == '\a') {
|
|
rc = YamlEmitBytes(pE, "\\a", 2); ++i;
|
|
} else if(c == '\b') {
|
|
rc = YamlEmitBytes(pE, "\\b", 2); ++i;
|
|
} else if(c == '\t') {
|
|
rc = YamlEmitBytes(pE, "\\t", 2); ++i;
|
|
} else if(c == '\n') {
|
|
rc = YamlEmitBytes(pE, "\\n", 2); ++i;
|
|
} else if(c == '\v') {
|
|
rc = YamlEmitBytes(pE, "\\v", 2); ++i;
|
|
} else if(c == '\f') {
|
|
rc = YamlEmitBytes(pE, "\\f", 2); ++i;
|
|
} else if(c == '\r') {
|
|
rc = YamlEmitBytes(pE, "\\r", 2); ++i;
|
|
} else if(c == 0x1B) {
|
|
rc = YamlEmitBytes(pE, "\\e", 2); ++i;
|
|
} else if(c >= 0x80) {
|
|
nStart = i;
|
|
if(!YamlUtf8Next(p, n, &i, &cp)) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_SYNTAX,
|
|
"Invalid UTF-8 in YAML scalar", 0, 0,
|
|
pE->zPath ? pE->zPath : "$");
|
|
}
|
|
if(cp == 0x85) {
|
|
rc = YamlEmitBytes(pE, "\\N", 2);
|
|
} else if(cp == 0x2028) {
|
|
rc = YamlEmitBytes(pE, "\\L", 2);
|
|
} else if(cp == 0x2029) {
|
|
rc = YamlEmitBytes(pE, "\\P", 2);
|
|
} else if(!pE->pOpt->allowUnicode) {
|
|
rc = cp < 0x10000 ? YamlEmitHexEscape(pE, 'u', cp, 4)
|
|
: YamlEmitHexEscape(pE, 'U', cp, 8);
|
|
} else {
|
|
rc = YamlEmitBytes(pE, (const char *)p + nStart, i - nStart);
|
|
}
|
|
} else if(c < 0x20 || c == 0x7F) {
|
|
rc = YamlEmitHexEscape(pE, 'x', c, 2);
|
|
++i;
|
|
} else {
|
|
rc = YamlEmitCh(pE, (char)c);
|
|
++i;
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
return YamlEmitCh(pE, '"');
|
|
}
|
|
static int YamlEmitSingleQuoted(yaml_emit *pE, const char *z, int n) {
|
|
int i, rc = YamlEmitCh(pE, '\'');
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
for(i = 0 ; i < n ; ++i) {
|
|
if(z[i] == '\'') {
|
|
rc = YamlEmitBytes(pE, "''", 2);
|
|
} else {
|
|
rc = YamlEmitCh(pE, z[i]);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
return YamlEmitCh(pE, '\'');
|
|
}
|
|
/* Is z[] safe as a plain scalar? NEL/LS/PS and non-ASCII with
|
|
* AllowUnicode=false force double quoting (escaped round-trip). In flow
|
|
* context the flow indicators must be quoted even inside a scalar. */
|
|
static int YamlPlainSafe(yaml_emit *pE, const char *z, int n, int bFlow) {
|
|
const unsigned char *p = (const unsigned char *)z;
|
|
int i;
|
|
unsigned char c;
|
|
if(n < 1) {
|
|
return 0;
|
|
}
|
|
if(YamlIsSpaceTab(p[0]) || YamlIsSpaceTab(p[n - 1])) {
|
|
return 0;
|
|
}
|
|
c = p[0];
|
|
if(c == '-' || c == '?' || c == ':' || c == ',' || c == '[' || c == ']' ||
|
|
c == '{' || c == '}' || c == '#' || c == '&' || c == '*' || c == '!' ||
|
|
c == '|' || c == '>' || c == '\'' || c == '"' || c == '%' || c == '@' ||
|
|
c == '`') {
|
|
return 0;
|
|
}
|
|
if(n == 3 && (SyStrncmp(z, "---", 3) == 0 || SyStrncmp(z, "...", 3) == 0)) {
|
|
return 0;
|
|
}
|
|
for(i = 0 ; i < n ; ++i) {
|
|
c = p[i];
|
|
if(c < 0x20 || c == 0x7F) {
|
|
return 0;
|
|
}
|
|
if(c == ':' && (i + 1 == n || YamlIsSpaceTab(p[i + 1]))) {
|
|
return 0;
|
|
}
|
|
if(c == '#' && i > 0 && YamlIsSpaceTab(p[i - 1])) {
|
|
return 0;
|
|
}
|
|
if(bFlow && (c == ',' || c == '[' || c == ']' || c == '{' || c == '}')) {
|
|
return 0;
|
|
}
|
|
if(c >= 0x80) {
|
|
sxu32 cp;
|
|
if(!YamlUtf8Next(p, n, &i, &cp)) {
|
|
return 0;
|
|
}
|
|
if(cp == 0x85 || cp == 0x2028 || cp == 0x2029 || !pE->pOpt->allowUnicode) {
|
|
return 0;
|
|
}
|
|
--i; /* YamlUtf8Next already advanced past this code point */
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
static int YamlEmitText(yaml_emit *pE, const char *z, int n, int bFlow) {
|
|
const unsigned char *p = (const unsigned char *)z;
|
|
int i;
|
|
if(YamlPlainSafe(pE, z, n, bFlow)) {
|
|
return YamlEmitBytes(pE, z, n);
|
|
}
|
|
for(i = 0 ; i < n ; ++i) {
|
|
if(p[i] < 0x20 || p[i] == 0x7F || p[i] >= 0x80) {
|
|
return YamlEmitDoubleQuoted(pE, z, n);
|
|
}
|
|
}
|
|
return YamlEmitSingleQuoted(pE, z, n);
|
|
}
|
|
/* Keys are literal lexemes. Quote any key that would resolve to a non-string
|
|
* (numbers, booleans, null...) or that needs quoting for syntax reasons. */
|
|
static int YamlEmitKey(yaml_emit *pE, const char *z, int n, int bFlow) {
|
|
yaml_scanner s;
|
|
int nResolve, i;
|
|
const unsigned char *p = (const unsigned char *)z;
|
|
SyZero(&s, sizeof(s));
|
|
s.pOpt = pE->pOpt;
|
|
nResolve = YamlResolveKind(&s, z, n);
|
|
if(nResolve == YAML_NODE_STRING && YamlPlainSafe(pE, z, n, bFlow)) {
|
|
return YamlEmitBytes(pE, z, n);
|
|
}
|
|
/* Single quotes cannot carry control characters, line breaks or raw
|
|
* non-ASCII bytes: the parser rejects them (AllowUnicode=false) or folds
|
|
* the break. Use the escaped double-quoted form just like YamlEmitText. */
|
|
for(i = 0 ; i < n ; ++i) {
|
|
if(p[i] < 0x20 || p[i] == 0x7F || p[i] >= 0x80) {
|
|
return YamlEmitDoubleQuoted(pE, z, n);
|
|
}
|
|
}
|
|
return YamlEmitSingleQuoted(pE, z, n);
|
|
}
|
|
static const char *YamlKindTagShort(int nKind) {
|
|
switch(nKind) {
|
|
case YAML_NODE_STRING: return "str";
|
|
case YAML_NODE_INT: return "int";
|
|
case YAML_NODE_FLOAT: return "float";
|
|
case YAML_NODE_BOOLEAN: return "bool";
|
|
case YAML_NODE_NULL_NODE: return "null";
|
|
case YAML_NODE_SEQUENCE: return "seq";
|
|
case YAML_NODE_MAPPING: return "map";
|
|
default: return 0;
|
|
}
|
|
}
|
|
/* Build the explicit tag text into pTag. A tag stored in the DOM is always
|
|
* emitted: !<full-uri> for custom tags, !!short for the seven standard short
|
|
* names, !<tag:yaml.org,2002:other> for any other standard suffix. When no tag
|
|
* was stored, emit one only if the value would otherwise resolve differently. */
|
|
static int YamlEmitTag(yaml_emit *pE, ph7_value *pNode, int nKind,
|
|
const char *zText, int nText, SyBlob *pTag) {
|
|
const char *zTag;
|
|
int nTag = 0, rc = PH7_OK;
|
|
yaml_scanner s;
|
|
int nResolve;
|
|
SyBlobLength(pTag) = 0;
|
|
zTag = YamlFieldStr(pNode, "tag", &nTag);
|
|
if(nTag > 0) {
|
|
if(nTag > 18 && SyStrncmp(zTag, "tag:yaml.org,2002:", 18) == 0) {
|
|
int nShort = 0;
|
|
const char *zStd = YamlKindTagShort(
|
|
YamlStdTagKind(zTag + 18, nTag - 18));
|
|
if(zStd && (int)SyStrlen(zStd) == nTag - 18 &&
|
|
memcmp(zTag + 18, zStd, (sxu32)(nTag - 18)) == 0) {
|
|
nShort = 1;
|
|
}
|
|
if(nShort) {
|
|
rc = SyBlobAppend(pTag, "!!", 2) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, zTag + 18,
|
|
(sxu32)(nTag - 18)) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
} else {
|
|
rc = SyBlobAppend(pTag, "!<", 2) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, zTag, (sxu32)nTag) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, ">", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
}
|
|
} else {
|
|
rc = SyBlobAppend(pTag, "!<", 2) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, zTag, (sxu32)nTag) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, ">", 1) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
}
|
|
return rc;
|
|
}
|
|
if(nKind == YAML_NODE_MAPPING || nKind == YAML_NODE_SEQUENCE) {
|
|
return PH7_OK; /* collections resolve from their kind, never from text */
|
|
}
|
|
SyZero(&s, sizeof(s));
|
|
s.pOpt = pE->pOpt;
|
|
nResolve = YamlResolveKind(&s, zText, nText);
|
|
if(nResolve != nKind) {
|
|
const char *zShort = YamlKindTagShort(nKind);
|
|
if(zShort) {
|
|
rc = SyBlobAppend(pTag, "!!", 2) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
if(rc == PH7_OK) rc = SyBlobAppend(pTag, zShort, (sxu32)SyStrlen(zShort)) == SXRET_OK ? PH7_OK : PH7_ABORT;
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
/* Emit only the prepared tag text (no surrounding spaces). */
|
|
static int YamlEmitTagOnly(yaml_emit *pE, SyBlob *pTag) {
|
|
if(SyBlobLength(pTag) < 1) {
|
|
return PH7_OK;
|
|
}
|
|
return YamlEmitBytes(pE, (const char *)SyBlobData(pTag), (int)SyBlobLength(pTag));
|
|
}
|
|
/* Emit the separator before a value, the tag (if any) and the separator
|
|
* after it. bLeadSpace is 0 for a root node with no ": " / "- " prefix. */
|
|
static int YamlEmitTagPrefix(yaml_emit *pE, SyBlob *pTag, int bLeadSpace) {
|
|
int rc = PH7_OK;
|
|
if(bLeadSpace) {
|
|
rc = YamlEmitCh(pE, ' ');
|
|
}
|
|
if(rc == PH7_OK) rc = YamlEmitTagOnly(pE, pTag);
|
|
if(rc == PH7_OK && SyBlobLength(pTag) > 0) rc = YamlEmitCh(pE, ' ');
|
|
return rc;
|
|
}
|
|
struct YamlEmitWalk {
|
|
yaml_emit *pE;
|
|
int nDepth;
|
|
const char *zPath;
|
|
int rc;
|
|
};
|
|
static int YamlEmitValueBody(yaml_emit *pE, ph7_value *pNode, int nDepth, int bLeadSpace);
|
|
static int YamlEmitMapWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlEmitWalk *pW = (struct YamlEmitWalk *)pUserData;
|
|
const char *zKey;
|
|
int nKey = 0, rc;
|
|
zKey = YamlKeyDecode(pKey, &nKey);
|
|
pW->pE->zPath = pW->zPath ? pW->zPath : "$";
|
|
rc = YamlEmitIndent(pW->pE, pW->nDepth);
|
|
if(rc == PH7_OK) rc = YamlEmitKey(pW->pE, zKey, nKey, 0);
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pW->pE, ':');
|
|
if(rc == PH7_OK) rc = YamlEmitValueBody(pW->pE, pValue, pW->nDepth, 1);
|
|
pW->rc = rc;
|
|
return rc == PH7_OK ? 0 : 1;
|
|
}
|
|
static int YamlEmitSeqWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlEmitWalk *pW = (struct YamlEmitWalk *)pUserData;
|
|
int rc;
|
|
SXUNUSED(pKey);
|
|
pW->pE->zPath = pW->zPath ? pW->zPath : "$";
|
|
rc = YamlEmitIndent(pW->pE, pW->nDepth);
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pW->pE, '-');
|
|
if(rc == PH7_OK) rc = YamlEmitValueBody(pW->pE, pValue, pW->nDepth, 1);
|
|
pW->rc = rc;
|
|
return rc == PH7_OK ? 0 : 1;
|
|
}
|
|
/* Emit a scalar node (tag prefix, text and optional trailing LF). Shared by
|
|
* the block and flow emitter paths. */
|
|
static int YamlEmitScalarInline(yaml_emit *pE, ph7_value *pNode, int nKind,
|
|
SyBlob *pTag, int bLeadSpace, int bNewline, int bFlow) {
|
|
const char *zText, *zNum;
|
|
int nText = 0, nNum = 0, n, rc = PH7_OK;
|
|
char zNumBuf[64];
|
|
zText = YamlFieldStr(pNode, "text", &nText);
|
|
if(nKind == YAML_NODE_INT) {
|
|
ph7_int64 iVal = 0;
|
|
zNum = YamlFieldStr(pNode, "rawNumber", &nNum);
|
|
if(nNum < 1 || !YamlLexemeInt64(zNum, (sxu32)nNum, &iVal)) {
|
|
iVal = YamlFieldInt(pNode, "intValue");
|
|
n = snprintf(zNumBuf, sizeof(zNumBuf), "%lld", (long long)iVal);
|
|
zNum = zNumBuf;
|
|
nNum = n < 0 ? 0 : n;
|
|
}
|
|
zText = zNum;
|
|
nText = nNum;
|
|
} else if(nKind == YAML_NODE_FLOAT) {
|
|
double rVal = YamlFieldDouble(pNode, "realValue");
|
|
zNum = YamlFieldStr(pNode, "rawNumber", &nNum);
|
|
if(nNum < 1 || !YamlParseFloat(zNum, nNum, &rVal)) {
|
|
if(isinf(rVal)) {
|
|
zNum = rVal > 0 ? ".inf" : "-.inf";
|
|
nNum = (int)SyStrlen(zNum);
|
|
} else if(isnan(rVal)) {
|
|
zNum = ".nan";
|
|
nNum = 4;
|
|
} else if(YamlFormatReal(zNumBuf, sizeof(zNumBuf), (ph7_real)rVal) == SXRET_OK) {
|
|
zNum = zNumBuf;
|
|
nNum = (int)SyStrlen(zNum);
|
|
} else {
|
|
zNum = "0.0";
|
|
nNum = 3;
|
|
}
|
|
}
|
|
zText = zNum;
|
|
nText = nNum;
|
|
} else if(nKind == YAML_NODE_BOOLEAN) {
|
|
zText = YamlFieldBool(pNode, "boolValue") ? "true" : "false";
|
|
nText = (int)SyStrlen(zText);
|
|
} else if(nKind == YAML_NODE_NULL_NODE) {
|
|
zText = "null";
|
|
nText = 4;
|
|
}
|
|
rc = YamlEmitTag(pE, pNode, nKind, zText, nText, pTag);
|
|
if(rc == PH7_OK) rc = YamlEmitTagPrefix(pE, pTag, bLeadSpace);
|
|
if(rc == PH7_OK) rc = YamlEmitText(pE, zText, nText, bFlow);
|
|
if(rc == PH7_OK && bNewline) rc = YamlEmitCh(pE, '\n');
|
|
return rc;
|
|
}
|
|
/* Emit one node value. The caller has already emitted indentation and the
|
|
* ":" / "-" prefix. Returns PH7_OK or a thrown-exception code. */
|
|
static int YamlEmitValueBody(yaml_emit *pE, ph7_value *pNode, int nDepth, int bLeadSpace) {
|
|
ph7_context *pCtx = pE->pCtx;
|
|
const char *zPath;
|
|
int nKind, nCount, rc = PH7_OK, push;
|
|
SyBlob sTag;
|
|
int bTagInit = 0;
|
|
ph7_value *pChildren;
|
|
struct YamlEmitWalk sWalk;
|
|
zPath = YamlFieldStr(pNode, "path", 0);
|
|
pE->zPath = zPath ? zPath : "$";
|
|
if(pNode == 0 || !ph7_value_is_object(pNode) || pNode->x.pOther == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0,
|
|
zPath ? zPath : "$");
|
|
}
|
|
if(nDepth > pE->pOpt->maxDepth) {
|
|
return YamlThrow(pCtx, YAML_ERROR_DEPTH, "Maximum depth exceeded", 0, 0,
|
|
zPath ? zPath : "$");
|
|
}
|
|
push = YamlEmitPush(pE, pNode);
|
|
if(push == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_INVALID_OPERATION,
|
|
"Cannot serialize a cyclic node graph", 0, 0, zPath ? zPath : "$");
|
|
}
|
|
if(push < 0) {
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(SyBlobInit(&sTag, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
YamlEmitPop(pE);
|
|
return PH7_ABORT;
|
|
}
|
|
bTagInit = 1;
|
|
nKind = (int)YamlFieldInt(pNode, "kind");
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
nCount = pChildren ? (int)ph7_array_count(pChildren) : 0;
|
|
if(nKind == YAML_NODE_MAPPING || nKind == YAML_NODE_SEQUENCE) {
|
|
rc = YamlEmitTag(pE, pNode, nKind, "", 0, &sTag);
|
|
if(nCount == 0) {
|
|
if(rc == PH7_OK) rc = YamlEmitTagPrefix(pE, &sTag, bLeadSpace);
|
|
if(rc == PH7_OK) {
|
|
rc = YamlEmitBytes(pE, nKind == YAML_NODE_MAPPING ? "{}\n" : "[]\n", -1);
|
|
}
|
|
} else {
|
|
/* Block collection: the ':' / '-' prefix is followed directly by
|
|
* a newline (or by " !<tag>" then a newline), never by a space. */
|
|
if(rc == PH7_OK && SyBlobLength(&sTag) > 0) {
|
|
rc = YamlEmitCh(pE, ' ');
|
|
if(rc == PH7_OK) rc = YamlEmitTagOnly(pE, &sTag);
|
|
}
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pE, '\n');
|
|
if(rc == PH7_OK) {
|
|
sWalk.pE = pE;
|
|
sWalk.nDepth = nDepth + 1;
|
|
sWalk.zPath = zPath;
|
|
sWalk.rc = PH7_OK;
|
|
(void)ph7_array_walk(pChildren,
|
|
nKind == YAML_NODE_MAPPING ? YamlEmitMapWalker : YamlEmitSeqWalker, &sWalk);
|
|
rc = sWalk.rc;
|
|
}
|
|
}
|
|
} else {
|
|
rc = YamlEmitScalarInline(pE, pNode, nKind, &sTag, bLeadSpace, 1, 0);
|
|
}
|
|
if(bTagInit) {
|
|
SyBlobRelease(&sTag);
|
|
}
|
|
YamlEmitPop(pE);
|
|
return rc;
|
|
}
|
|
/* Flow-style emitter: the whole document is one flow collection. */
|
|
static int YamlEmitFlowValue(yaml_emit *pE, ph7_value *pNode, int nDepth, int bLeadSpace);
|
|
struct YamlEmitFlowWalk {
|
|
yaml_emit *pE;
|
|
int nDepth;
|
|
int bFirst;
|
|
const char *zPath;
|
|
int rc;
|
|
};
|
|
static int YamlEmitFlowMapWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlEmitFlowWalk *pW = (struct YamlEmitFlowWalk *)pUserData;
|
|
const char *zKey;
|
|
int nKey = 0, rc;
|
|
zKey = YamlKeyDecode(pKey, &nKey);
|
|
pW->pE->zPath = pW->zPath ? pW->zPath : "$";
|
|
if(!pW->bFirst) {
|
|
rc = YamlEmitBytes(pW->pE, ", ", 2);
|
|
if(rc != PH7_OK) { pW->rc = rc; return 1; }
|
|
}
|
|
pW->bFirst = 0;
|
|
rc = YamlEmitKey(pW->pE, zKey, nKey, 1);
|
|
if(rc == PH7_OK) rc = YamlEmitBytes(pW->pE, ": ", 2);
|
|
if(rc == PH7_OK) rc = YamlEmitFlowValue(pW->pE, pValue, pW->nDepth, 0);
|
|
pW->rc = rc;
|
|
return rc == PH7_OK ? 0 : 1;
|
|
}
|
|
static int YamlEmitFlowSeqWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlEmitFlowWalk *pW = (struct YamlEmitFlowWalk *)pUserData;
|
|
int rc;
|
|
SXUNUSED(pKey);
|
|
pW->pE->zPath = pW->zPath ? pW->zPath : "$";
|
|
if(!pW->bFirst) {
|
|
rc = YamlEmitBytes(pW->pE, ", ", 2);
|
|
if(rc != PH7_OK) { pW->rc = rc; return 1; }
|
|
}
|
|
pW->bFirst = 0;
|
|
rc = YamlEmitFlowValue(pW->pE, pValue, pW->nDepth, 0);
|
|
pW->rc = rc;
|
|
return rc == PH7_OK ? 0 : 1;
|
|
}
|
|
static int YamlEmitFlowValue(yaml_emit *pE, ph7_value *pNode, int nDepth, int bLeadSpace) {
|
|
const char *zPath;
|
|
int nKind, nCount, rc = PH7_OK, push;
|
|
ph7_value *pChildren;
|
|
SyBlob sTag;
|
|
int bTagInit = 0;
|
|
struct YamlEmitFlowWalk sWalk;
|
|
if(pNode == 0 || !ph7_value_is_object(pNode) || pNode->x.pOther == 0) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0, "$");
|
|
}
|
|
zPath = YamlFieldStr(pNode, "path", 0);
|
|
pE->zPath = zPath ? zPath : "$";
|
|
if(nDepth > pE->pOpt->maxDepth) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_DEPTH, "Maximum depth exceeded", 0, 0,
|
|
zPath ? zPath : "$");
|
|
}
|
|
push = YamlEmitPush(pE, pNode);
|
|
if(push == 0) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_INVALID_OPERATION,
|
|
"Cannot serialize a cyclic node graph", 0, 0, zPath ? zPath : "$");
|
|
}
|
|
if(push < 0) {
|
|
PH7_VmMemoryError(pE->pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(SyBlobInit(&sTag, &pE->pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
YamlEmitPop(pE);
|
|
return PH7_ABORT;
|
|
}
|
|
bTagInit = 1;
|
|
nKind = (int)YamlFieldInt(pNode, "kind");
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
nCount = pChildren ? (int)ph7_array_count(pChildren) : 0;
|
|
if(nKind == YAML_NODE_MAPPING || nKind == YAML_NODE_SEQUENCE) {
|
|
rc = YamlEmitTag(pE, pNode, nKind, "", 0, &sTag);
|
|
if(rc == PH7_OK) rc = YamlEmitTagPrefix(pE, &sTag, bLeadSpace);
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pE, nKind == YAML_NODE_MAPPING ? '{' : '[');
|
|
if(rc == PH7_OK && nCount > 0) {
|
|
sWalk.pE = pE;
|
|
sWalk.nDepth = nDepth + 1;
|
|
sWalk.bFirst = 1;
|
|
sWalk.zPath = zPath;
|
|
sWalk.rc = PH7_OK;
|
|
(void)ph7_array_walk(pChildren,
|
|
nKind == YAML_NODE_MAPPING ? YamlEmitFlowMapWalker : YamlEmitFlowSeqWalker, &sWalk);
|
|
rc = sWalk.rc;
|
|
}
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pE, nKind == YAML_NODE_MAPPING ? '}' : ']');
|
|
} else {
|
|
rc = YamlEmitScalarInline(pE, pNode, nKind, &sTag, bLeadSpace, 0, 1);
|
|
}
|
|
if(bTagInit) SyBlobRelease(&sTag);
|
|
YamlEmitPop(pE);
|
|
return rc;
|
|
}
|
|
/* Emit one whole document in flow style. */
|
|
static int YamlEmitFlowRoot(yaml_emit *pE, ph7_value *pRoot) {
|
|
int rc = YamlEmitFlowValue(pE, pRoot, 0, 0);
|
|
if(rc == PH7_OK) rc = YamlEmitCh(pE, '\n');
|
|
return rc;
|
|
}
|
|
/* Emit a whole document root (no key/sequence prefix). */
|
|
static int YamlEmitRoot(yaml_emit *pE, ph7_value *pRoot) {
|
|
int nKind, nCount, rc = PH7_OK, push;
|
|
ph7_value *pChildren;
|
|
const char *zPath;
|
|
SyBlob sTag;
|
|
int bTagInit = 0;
|
|
if(pRoot == 0 || !ph7_value_is_object(pRoot) || pRoot->x.pOther == 0) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_TYPE_MISMATCH,
|
|
"YamlDocument has no valid root", 0, 0, "$");
|
|
}
|
|
zPath = YamlFieldStr(pRoot, "path", 0);
|
|
pE->zPath = zPath ? zPath : "$";
|
|
if(pE->pOpt->style == YAML_STYLE_FLOW) {
|
|
return YamlEmitFlowRoot(pE, pRoot);
|
|
}
|
|
nKind = (int)YamlFieldInt(pRoot, "kind");
|
|
pChildren = YamlFieldContainer(pRoot, "children");
|
|
nCount = pChildren ? (int)ph7_array_count(pChildren) : 0;
|
|
if((nKind == YAML_NODE_MAPPING || nKind == YAML_NODE_SEQUENCE) && nCount > 0) {
|
|
push = YamlEmitPush(pE, pRoot);
|
|
if(push == 0) {
|
|
return YamlThrow(pE->pCtx, YAML_ERROR_INVALID_OPERATION,
|
|
"Cannot serialize a cyclic node graph", 0, 0, zPath ? zPath : "$");
|
|
}
|
|
if(push < 0) {
|
|
PH7_VmMemoryError(pE->pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(SyBlobInit(&sTag, &pE->pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
YamlEmitPop(pE);
|
|
return PH7_ABORT;
|
|
}
|
|
bTagInit = 1;
|
|
rc = YamlEmitTag(pE, pRoot, nKind, "", 0, &sTag);
|
|
if(rc == PH7_OK) rc = YamlEmitTagOnly(pE, &sTag);
|
|
/* A tagged root needs the tag on its own line; an untagged root
|
|
* starts directly with its first entry. No leading separator. */
|
|
if(rc == PH7_OK && SyBlobLength(&sTag) > 0) rc = YamlEmitCh(pE, '\n');
|
|
if(rc == PH7_OK) {
|
|
struct YamlEmitWalk sWalk;
|
|
sWalk.pE = pE;
|
|
sWalk.nDepth = 0;
|
|
sWalk.zPath = zPath;
|
|
sWalk.rc = PH7_OK;
|
|
(void)ph7_array_walk(pChildren,
|
|
nKind == YAML_NODE_MAPPING ? YamlEmitMapWalker : YamlEmitSeqWalker, &sWalk);
|
|
rc = sWalk.rc;
|
|
}
|
|
if(bTagInit) SyBlobRelease(&sTag);
|
|
YamlEmitPop(pE);
|
|
} else {
|
|
/* Empty root collection or scalar: the value path performs its own
|
|
* cycle push/pop, so do not push here as well. */
|
|
rc = YamlEmitValueBody(pE, pRoot, 0, 0);
|
|
}
|
|
return rc;
|
|
}
|
|
/* Serialize one node into a freshly allocated result string. */
|
|
static int YamlSerialize(ph7_context *pCtx, ph7_value *pNode, ph7_value *pOptions) {
|
|
yaml_opt sOpt;
|
|
yaml_emit sE;
|
|
int rc;
|
|
rc = YamlOptLoad(pCtx, pOptions, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = YamlEmitInit(&sE, pCtx, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
if(sOpt.explicitDocumentStart) {
|
|
rc = YamlEmitBytes(&sE, "---\n", 4);
|
|
}
|
|
if(rc == PH7_OK) {
|
|
if(pNode == 0) {
|
|
rc = YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0, "$");
|
|
} else {
|
|
rc = YamlEmitRoot(&sE, pNode);
|
|
}
|
|
}
|
|
if(rc == PH7_OK && sOpt.explicitDocumentEnd) {
|
|
rc = YamlEmitBytes(&sE, "...\n", 4);
|
|
}
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_result_string(pCtx, (const char *)SyBlobData(&sE.sOut),
|
|
(int)SyBlobLength(&sE.sOut));
|
|
}
|
|
YamlEmitRelease(&sE);
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Method callbacks: YamlException */
|
|
/* ------------------------------------------------------------------ */
|
|
static int YamlExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pBase;
|
|
ph7_class_method *pCons;
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
/* Run the base constructor so message/code/file/line/trace/previous keep
|
|
* the ordinary Exception semantics for directly constructed instances. */
|
|
pBase = PH7_VmExtractClass(pVm, "Exception", sizeof("Exception") - 1, FALSE);
|
|
if(pBase == 0) return PH7_ABORT;
|
|
pCons = PH7_ClassExtractMethod(pBase, "__construct", sizeof("__construct") - 1);
|
|
if(pCons == 0) return PH7_ABORT;
|
|
rc = PH7_VmCallClassMethod(pVm, (ph7_class_instance *)pThis->x.pOther, pCons, 0, 2, apArg);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "inputLine", apArg[2]);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "inputColumn", apArg[3]);
|
|
if(rc != PH7_OK) return rc;
|
|
return ph7_context_attr_store(pCtx, pThis, 0, "path", apArg[4]);
|
|
}
|
|
static int YamlExceptionField(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) return PH7_ABORT;
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0,
|
|
(const char *)ph7_context_user_data(pCtx), pValue);
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
|
|
}
|
|
static int YamlExceptionDescription(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1;
|
|
return ph7_result_string(pCtx, YamlErrorDescription(nErr), -1);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Method callbacks: YamlOptions */
|
|
/* ------------------------------------------------------------------ */
|
|
static int YamlOptionsConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(pCtx); SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlOptionsMakeReadOnly(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
(void)YamlFieldSetBool(ph7_context_this(pCtx), "readOnly", 1);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlOptionsIsReadOnly(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return ph7_result_bool(pCtx, YamlFieldBool(ph7_context_this(pCtx), "readOnly"));
|
|
}
|
|
static int YamlOptionsValidate(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return YamlOptValidate(pCtx, ph7_context_this(pCtx));
|
|
}
|
|
static int YamlOptionsCopy(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_class *pClass = PH7_VmExtractClass(pVm, YCLS_OPTIONS, sizeof(YCLS_OPTIONS) - 1, FALSE);
|
|
ph7_class_instance *pInst;
|
|
ph7_value *pCopy;
|
|
static const char *const azBool[] = {
|
|
"allowUnicode", "allowAliases", "explicitDocumentStart", "explicitDocumentEnd"
|
|
};
|
|
static const char *const azInt[] = {
|
|
"maxAliasExpansion", "duplicateKeyPolicy", "unknownTagPolicy", "schema",
|
|
"maxDepth", "indent", "style"
|
|
};
|
|
sxu32 i;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if(pClass == 0) return PH7_ABORT;
|
|
pCopy = ph7_context_new_scalar(pCtx);
|
|
if(pCopy == 0) return PH7_ABORT;
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
ph7_context_release_value(pCtx, pCopy);
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(pInst->iRef < 1) pInst->iRef = 1;
|
|
PH7_MemObjRelease(pCopy);
|
|
pCopy->x.pOther = pInst;
|
|
MemObjSetType(pCopy, MEMOBJ_OBJ);
|
|
for(i = 0 ; i < SX_ARRAYSIZE(azBool) ; ++i) {
|
|
YamlFieldSetBool(pCopy, azBool[i], YamlFieldBool(pThis, azBool[i]));
|
|
}
|
|
for(i = 0 ; i < SX_ARRAYSIZE(azInt) ; ++i) {
|
|
YamlFieldSetInt(pCopy, azInt[i], YamlFieldInt(pThis, azInt[i]));
|
|
}
|
|
YamlFieldSetBool(pCopy, "readOnly", 0);
|
|
return ph7_result_value(pCtx, pCopy);
|
|
}
|
|
static int YamlOptionsGet(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
const char *zName = (const char *)ph7_context_user_data(pCtx);
|
|
ph7_value *pField = YamlField(pThis, zName);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if(pField == 0) return PH7_CORRUPT;
|
|
return ph7_result_value(pCtx, pField);
|
|
}
|
|
static int YamlOptionsSetBool(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
const char *zName = (const char *)ph7_context_user_data(pCtx);
|
|
int bVal;
|
|
if(YamlFieldBool(pThis, "readOnly")) {
|
|
return YamlThrowOption(pCtx, YAML_ERROR_READONLY_OPTION,
|
|
"YamlOptions is read-only; use Copy() to derive a modifiable instance");
|
|
}
|
|
bVal = nArg > 0 ? ph7_value_to_bool(apArg[0]) : 0;
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
return YamlFieldSetBool(pThis, zName, bVal);
|
|
}
|
|
static int YamlOptionsSetInt(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
const char *zName = (const char *)ph7_context_user_data(pCtx);
|
|
ph7_int64 iVal;
|
|
if(YamlFieldBool(pThis, "readOnly")) {
|
|
return YamlThrowOption(pCtx, YAML_ERROR_READONLY_OPTION,
|
|
"YamlOptions is read-only; use Copy() to derive a modifiable instance");
|
|
}
|
|
iVal = nArg > 0 ? ph7_value_to_int64(apArg[0]) : 0;
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
if(strcmp(zName, "maxDepth") == 0) {
|
|
if(iVal < 1 || iVal > YAML_DEFAULT_MAX_DEPTH) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.MaxDepth must be in range 1..512 (got %d)", (int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "maxAliasExpansion") == 0) {
|
|
if(iVal < 1 || iVal > YAML_MAX_ALIAS_EXPANSION_LIMIT) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.MaxAliasExpansion must be in range 1..1000000 (got %d)", (int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "duplicateKeyPolicy") == 0) {
|
|
if(iVal != YAML_DUP_KEEP_LAST && iVal != YAML_DUP_KEEP_FIRST && iVal != YAML_DUP_REJECT) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.DuplicateKeyPolicy must be YamlOptions.KEEP_LAST, KEEP_FIRST or REJECT (got %d)",
|
|
(int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "unknownTagPolicy") == 0) {
|
|
if(iVal != YAML_UNKNOWN_TAG_REJECT && iVal != YAML_UNKNOWN_TAG_KEEP) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.UnknownTagPolicy must be YamlOptions.UNKNOWN_TAG_REJECT or UNKNOWN_TAG_KEEP (got %d)",
|
|
(int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "schema") == 0) {
|
|
if(iVal != YAML_SCHEMA_CORE && iVal != YAML_SCHEMA_JSON && iVal != YAML_SCHEMA_FAILSAFE) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Schema must be YamlOptions.SCHEMA_CORE, SCHEMA_JSON or SCHEMA_FAILSAFE (got %d)",
|
|
(int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "style") == 0) {
|
|
if(iVal != YAML_STYLE_BLOCK && iVal != YAML_STYLE_FLOW) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Style must be YamlOptions.STYLE_BLOCK or STYLE_FLOW (got %d)",
|
|
(int)iVal);
|
|
}
|
|
} else if(strcmp(zName, "indent") == 0) {
|
|
if(iVal < 2 || iVal > 9) {
|
|
return YamlThrowFmt(pCtx, YAML_ERROR_INVALID_OPTION,
|
|
"YamlOptions.Indent must be an integer in range 2..9 (got %d)", (int)iVal);
|
|
}
|
|
}
|
|
return YamlFieldSetInt(pThis, zName, iVal);
|
|
}
|
|
/* ------------------------------------------------------------------ */
|
|
/* Method callbacks: YamlNode getters */
|
|
/* ------------------------------------------------------------------ */
|
|
static int YamlNodeConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int nKind = (int)(long)ph7_context_user_data(pCtx);
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
(void)YamlFieldSetInt(pThis, "kind", nKind);
|
|
(void)YamlFieldSetStr(pThis, "path", "$", -1);
|
|
(void)YamlFieldSetInt(pThis, "depth", 0);
|
|
(void)YamlFieldSetStr(pThis, "text", "", 0);
|
|
(void)YamlFieldSetInt(pThis, "intValue", 0);
|
|
(void)YamlFieldSetDouble(pThis, "realValue", 0.0);
|
|
(void)YamlFieldSetBool(pThis, "boolValue", 0);
|
|
(void)YamlFieldSetStr(pThis, "rawNumber", "", 0);
|
|
(void)YamlFieldSetStr(pThis, "tag", "", 0);
|
|
{
|
|
ph7_value *pArray = ph7_context_new_array(pCtx);
|
|
if(pArray == 0) return PH7_ABORT;
|
|
(void)YamlFieldSetObj(pThis, "children", pArray);
|
|
ph7_context_release_value(pCtx, pArray);
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeCtorString(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc = YamlNodeConstruct(pCtx, nArg, apArg);
|
|
const char *zVal;
|
|
int nByte = 0;
|
|
if(rc != PH7_OK) return rc;
|
|
zVal = nArg > 0 ? ph7_value_to_string(apArg[0], &nByte) : "";
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
(void)YamlFieldSetStr(ph7_context_this(pCtx), "text", zVal ? zVal : "", nByte);
|
|
/* A string node's raw form is the text itself. */
|
|
(void)YamlFieldSetStr(ph7_context_this(pCtx), "rawNumber", zVal ? zVal : "", nByte);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeCtorInt(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc = YamlNodeConstruct(pCtx, nArg, apArg);
|
|
ph7_int64 iVal = nArg > 0 ? ph7_value_to_int64(apArg[0]) : 0;
|
|
ph7_value *pThis;
|
|
if(rc != PH7_OK) return rc;
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
pThis = ph7_context_this(pCtx);
|
|
YamlFieldSetInt(pThis, "intValue", iVal);
|
|
YamlFieldSetDouble(pThis, "realValue", (double)iVal);
|
|
{
|
|
char zBuf[32];
|
|
int n = snprintf(zBuf, sizeof(zBuf), "%lld", (long long)iVal);
|
|
YamlFieldSetStr(pThis, "rawNumber", zBuf, n);
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeCtorFloat(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc = YamlNodeConstruct(pCtx, nArg, apArg);
|
|
double rVal = nArg > 0 ? ph7_value_to_double(apArg[0]) : 0.0;
|
|
char zBuf[64];
|
|
if(rc != PH7_OK) return rc;
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
if(YamlFormatReal(zBuf, sizeof(zBuf), (ph7_real)rVal) != SXRET_OK) {
|
|
return YamlThrow(pCtx, YAML_ERROR_INVALID_OPERATION,
|
|
"Cannot represent NaN or infinity as a YAML number", 0, 0,
|
|
YamlFieldStr(ph7_context_this(pCtx), "path", 0));
|
|
}
|
|
{
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
YamlFieldSetDouble(pThis, "realValue", rVal);
|
|
YamlFieldSetStr(pThis, "rawNumber", zBuf, (int)SyStrlen(zBuf));
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeCtorBool(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc = YamlNodeConstruct(pCtx, nArg, apArg);
|
|
int bVal = nArg > 0 ? ph7_value_to_bool(apArg[0]) : 0;
|
|
if(rc != PH7_OK) return rc;
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
YamlFieldSetBool(ph7_context_this(pCtx), "boolValue", bVal);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeGetIntField(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
const char *zName = (const char *)ph7_context_user_data(pCtx);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return ph7_result_int64(pCtx, YamlFieldInt(ph7_context_this(pCtx), zName));
|
|
}
|
|
static int YamlNodeCheckKind2(ph7_context *pCtx, int nKind1, int nKind2, const char *zMethod) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
int nKind = (int)YamlFieldInt(pThis, "kind");
|
|
if(nKind != nKind1 && nKind != nKind2) {
|
|
const char *zPath = YamlFieldStr(pThis, "path", 0);
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, zMethod, 0, 0, zPath ? zPath : "$");
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeCheckKind(ph7_context *pCtx, int nKind, const char *zMethod) {
|
|
return YamlNodeCheckKind2(pCtx, nKind, nKind, zMethod);
|
|
}
|
|
/* GetInt() accepts an INT node only when its lexeme is exactly an int64. */
|
|
static int YamlNodeGetInt(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_int64 iVal;
|
|
const char *zSrc = YamlFieldStr(pThis, "path", 0);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if((int)YamlFieldInt(pThis, "kind") != YAML_NODE_INT) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH,
|
|
"GetInt requires a YamlIntNode", 0, 0, zSrc ? zSrc : "$");
|
|
}
|
|
{
|
|
int nLen;
|
|
const char *zRaw = YamlFieldStr(pThis, "rawNumber", &nLen);
|
|
if(YamlLexemeInt64(zRaw, (sxu32)nLen, &iVal)) {
|
|
return ph7_result_int64(pCtx, iVal);
|
|
}
|
|
}
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH,
|
|
"GetInt requires a number exactly representable as int64", 0, 0, zSrc ? zSrc : "$");
|
|
}
|
|
static int YamlNodeReturnStr(ph7_context *pCtx, const char *zName) {
|
|
ph7_value *pField = YamlField(ph7_context_this(pCtx), zName);
|
|
return pField ? ph7_result_value(pCtx, pField) : ph7_result_string(pCtx, "", 0);
|
|
}
|
|
/* GetPath/GetDepth/GetTag stay unchecked; the typed getters require their
|
|
* exact node kind and carry the node path in the type-mismatch diagnostic. */
|
|
static int YamlNodeGetStrField(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return YamlNodeReturnStr(pCtx, (const char *)ph7_context_user_data(pCtx));
|
|
}
|
|
static int YamlNodeGetString(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_STRING, "GetString requires a YamlStringNode");
|
|
if(rc != PH7_OK) return rc;
|
|
return YamlNodeReturnStr(pCtx, "text");
|
|
}
|
|
/* GetTag returns the resolved/explicit tag recorded by the parser, or the
|
|
* empty string for an untagged node. */
|
|
static int YamlNodeGetTag(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return YamlNodeReturnStr(pCtx, "tag");
|
|
}
|
|
static int YamlNodeGetDouble(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
rc = YamlNodeCheckKind2(pCtx, YAML_NODE_FLOAT, YAML_NODE_INT,
|
|
"GetDouble requires a YamlFloatNode or YamlIntNode");
|
|
if(rc != PH7_OK) return rc;
|
|
return ph7_result_double(pCtx, YamlFieldDouble(ph7_context_this(pCtx), "realValue"));
|
|
}
|
|
static int YamlNodeGetBool(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_BOOLEAN, "GetBool requires a YamlBooleanNode");
|
|
if(rc != PH7_OK) return rc;
|
|
return ph7_result_bool(pCtx, YamlFieldBool(ph7_context_this(pCtx), "boolValue"));
|
|
}
|
|
static int YamlNodeGetRawValue(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int rc;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
rc = YamlNodeCheckKind2(pCtx, YAML_NODE_INT, YAML_NODE_FLOAT,
|
|
"GetRawValue requires a YamlIntNode or YamlFloatNode");
|
|
if(rc != PH7_OK) return rc;
|
|
return YamlNodeReturnStr(pCtx, "rawNumber");
|
|
}
|
|
static int YamlNodeIsNull(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return ph7_result_bool(pCtx,
|
|
(int)YamlFieldInt(ph7_context_this(pCtx), "kind") == YAML_NODE_NULL_NODE);
|
|
}
|
|
static int YamlNodeGetDepth(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
/* Depth is stored eagerly: a key such as "a.b" must not inflate it. */
|
|
return ph7_result_int64(pCtx, YamlFieldInt(ph7_context_this(pCtx), "depth"));
|
|
}
|
|
static int YamlNodeGetProperty(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pChildren, *pFound;
|
|
const char *zName;
|
|
int nName = 0, rc;
|
|
SXUNUSED(nArg);
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_MAPPING, "GetProperty requires a YamlMappingNode");
|
|
if(rc != PH7_OK) return rc;
|
|
zName = ph7_value_to_string(apArg[0], &nName);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
pFound = YamlMapFetch(pCtx, pChildren, zName, nName);
|
|
return pFound ? ph7_result_value(pCtx, pFound) : ph7_result_null(pCtx);
|
|
}
|
|
static int YamlNodeHasProperty(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pChildren;
|
|
const char *zName;
|
|
int nName = 0, rc;
|
|
SXUNUSED(nArg);
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_MAPPING, "HasProperty requires a YamlMappingNode");
|
|
if(rc != PH7_OK) return rc;
|
|
zName = ph7_value_to_string(apArg[0], &nName);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
return ph7_result_bool(pCtx, YamlMapFetch(pCtx, pChildren, zName, nName) ? 1 : 0);
|
|
}
|
|
struct YamlKeyWalk {
|
|
ph7_context *pCtx;
|
|
ph7_value *pOut;
|
|
};
|
|
/* Walk callbacks hand out short-lived temporaries. Copy into a stack scalar,
|
|
* insert that copy into the owned result array, then release it. */
|
|
static int YamlKeyWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlKeyWalk *pWalk = (struct YamlKeyWalk *)pUserData;
|
|
int nKey = 0;
|
|
const char *zKey;
|
|
ph7_value sTmp;
|
|
SXUNUSED(pValue);
|
|
/* Decode the internal key prefix so the original lexeme is exposed. */
|
|
zKey = YamlKeyDecode(pKey, &nKey);
|
|
if(ph7_context_status(pWalk->pCtx) != PH7_OK) {
|
|
return 1;
|
|
}
|
|
PH7_MemObjInit(pWalk->pCtx->pVm, &sTmp);
|
|
(void)ph7_value_string(&sTmp, zKey ? zKey : "", nKey);
|
|
(void)ph7_array_add_strkey_elem(pWalk->pOut, "", &sTmp);
|
|
PH7_MemObjRelease(&sTmp);
|
|
return 0;
|
|
}
|
|
static int YamlValueWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlKeyWalk *pWalk = (struct YamlKeyWalk *)pUserData;
|
|
ph7_value sTmp;
|
|
SXUNUSED(pKey);
|
|
PH7_MemObjInit(pWalk->pCtx->pVm, &sTmp);
|
|
(void)PH7_MemObjStore(pValue, &sTmp);
|
|
(void)ph7_array_add_strkey_elem(pWalk->pOut, "", &sTmp);
|
|
PH7_MemObjRelease(&sTmp);
|
|
return 0;
|
|
}
|
|
static int YamlNodeGetPropertyNames(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pChildren, *pOut;
|
|
struct YamlKeyWalk sWalk;
|
|
int rc;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_MAPPING, "GetPropertyNames requires a YamlMappingNode");
|
|
if(rc != PH7_OK) return rc;
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
pOut = ph7_context_new_array(pCtx);
|
|
if(pOut == 0) return PH7_ABORT;
|
|
sWalk.pCtx = pCtx;
|
|
sWalk.pOut = pOut;
|
|
if(pChildren) {
|
|
(void)ph7_array_walk(pChildren, YamlKeyWalker, &sWalk);
|
|
}
|
|
YamlStampArrayType(pCtx, pOut, 0);
|
|
return ph7_result_value(pCtx, pOut);
|
|
}
|
|
static int YamlNodeGetIndex(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis;
|
|
ph7_value *pChildren, *pFound;
|
|
ph7_int64 iIndex;
|
|
int rc;
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_SEQUENCE, "GetIndex requires a YamlSequenceNode");
|
|
if(rc != PH7_OK) return rc;
|
|
SXUNUSED(nArg);
|
|
pThis = ph7_context_this(pCtx);
|
|
iIndex = ph7_value_to_int64(apArg[0]);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
pFound = YamlArrayGet(pCtx, pChildren, iIndex);
|
|
if(pFound == 0) {
|
|
return ph7_result_null(pCtx);
|
|
}
|
|
{
|
|
int rcOut = ph7_result_value(pCtx, pFound);
|
|
ph7_context_release_value(pCtx, pFound);
|
|
return rcOut;
|
|
}
|
|
}
|
|
static int YamlNodeGetCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pChildren = YamlFieldContainer(pThis, "children");
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return ph7_result_int64(pCtx, pChildren ? (ph7_int64)ph7_array_count(pChildren) : 0);
|
|
}
|
|
static int YamlNodeGetChildren(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pChildren = YamlFieldContainer(pThis, "children");
|
|
ph7_value *pOut;
|
|
struct YamlKeyWalk sWalk;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
pOut = ph7_context_new_array(pCtx);
|
|
if(pOut == 0) return PH7_ABORT;
|
|
sWalk.pCtx = pCtx;
|
|
sWalk.pOut = pOut;
|
|
if(pChildren) {
|
|
(void)ph7_array_walk(pChildren, YamlValueWalker, &sWalk);
|
|
}
|
|
YamlStampArrayType(pCtx, pOut, YCLS_NODE);
|
|
return ph7_result_value(pCtx, pOut);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Method callbacks: YamlNode mutators */
|
|
/* ------------------------------------------------------------------ */
|
|
/* Node identity is the class instance behind the value. Search from the node
|
|
* being inserted toward the container: the opposite direction would also
|
|
* reject legitimate sharing of one node in two places. */
|
|
static int YamlNodeContains(ph7_value *pRoot, ph7_value *pTarget, int nDepth);
|
|
struct YamlContainsWalk {
|
|
ph7_value *pTarget;
|
|
int nDepth;
|
|
int bFound;
|
|
};
|
|
static int YamlContainsWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlContainsWalk *pWalk = (struct YamlContainsWalk *)pUserData;
|
|
SXUNUSED(pKey);
|
|
if(pWalk->bFound) return 1;
|
|
if(YamlNodeContains(pValue, pWalk->pTarget, pWalk->nDepth + 1)) {
|
|
pWalk->bFound = 1;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
static int YamlNodeDescendantsContain(ph7_value *pRoot, ph7_value *pTarget, int nDepth) {
|
|
ph7_value *pChildren = YamlFieldContainer(pRoot, "children");
|
|
struct YamlContainsWalk sWalk;
|
|
if(pChildren == 0) return 0;
|
|
sWalk.pTarget = pTarget;
|
|
sWalk.nDepth = nDepth;
|
|
sWalk.bFound = 0;
|
|
(void)ph7_array_walk(pChildren, YamlContainsWalker, &sWalk);
|
|
return sWalk.bFound;
|
|
}
|
|
static int YamlNodeContains(ph7_value *pRoot, ph7_value *pTarget, int nDepth) {
|
|
if(pRoot == 0 || pTarget == 0) return 0;
|
|
if((pRoot->nType & MEMOBJ_OBJ) == 0 || (pTarget->nType & MEMOBJ_OBJ) == 0) return 0;
|
|
if(pRoot->x.pOther == pTarget->x.pOther) return 1;
|
|
if(nDepth > YAML_DEFAULT_MAX_DEPTH) return 0;
|
|
return YamlNodeDescendantsContain(pRoot, pTarget, nDepth);
|
|
}
|
|
/* bCheckRoot=0 is for SetRoot: the node always "is" itself, only an existing
|
|
* cycle in its descendants must be rejected there. */
|
|
static int YamlCheckCycle(ph7_context *pCtx, ph7_value *pNode, ph7_value *pTarget, int bCheckRoot) {
|
|
int bFound = bCheckRoot ? YamlNodeContains(pNode, pTarget, 0)
|
|
: YamlNodeDescendantsContain(pNode, pTarget, 0);
|
|
if(bFound) {
|
|
return YamlThrow(pCtx, YAML_ERROR_INVALID_OPERATION,
|
|
"Cannot insert a node into its own subtree", 0, 0,
|
|
YamlFieldStr(pNode, "path", 0));
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
/* A mutator must reject an over-deep subtree before linking it: repath would
|
|
* otherwise throw after the insertion and leave a partially mutated tree.
|
|
* The walk is depth-bounded, so an existing cycle also terminates here. */
|
|
static int YamlSubtreeTooDeep(ph7_value *pNode, int nDepth);
|
|
struct YamlDepthWalk {
|
|
int nDepth;
|
|
int bTooDeep;
|
|
};
|
|
static int YamlDepthWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlDepthWalk *pWalk = (struct YamlDepthWalk *)pUserData;
|
|
SXUNUSED(pKey);
|
|
if(pWalk->bTooDeep || YamlSubtreeTooDeep(pValue, pWalk->nDepth + 1)) {
|
|
pWalk->bTooDeep = 1;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
static int YamlSubtreeTooDeep(ph7_value *pNode, int nDepth) {
|
|
ph7_value *pChildren;
|
|
struct YamlDepthWalk sWalk;
|
|
if(nDepth > YAML_DEFAULT_MAX_DEPTH) return 1;
|
|
if(pNode == 0 || (pNode->nType & MEMOBJ_OBJ) == 0 || pNode->x.pOther == 0) return 0;
|
|
pChildren = YamlFieldContainer(pNode, "children");
|
|
if(pChildren == 0) return 0;
|
|
sWalk.nDepth = nDepth;
|
|
sWalk.bTooDeep = 0;
|
|
(void)ph7_array_walk(pChildren, YamlDepthWalker, &sWalk);
|
|
return sWalk.bTooDeep;
|
|
}
|
|
static int YamlCheckDepth(ph7_context *pCtx, ph7_value *pNode, int nBaseDepth) {
|
|
if(YamlSubtreeTooDeep(pNode, nBaseDepth)) {
|
|
return YamlThrow(pCtx, YAML_ERROR_DEPTH, "Maximum depth exceeded", 0, 0,
|
|
YamlFieldStr(pNode, "path", 0));
|
|
}
|
|
return PH7_OK;
|
|
}
|
|
/* Repath only the freshly inserted child, using its parent's stored path.
|
|
* The argument value and the array entry share the same class instance. */
|
|
static int YamlRepathChild(ph7_context *pCtx, ph7_value *pParent, ph7_value *pChild,
|
|
const char *zKey, int nKey, int iIndex, int bIndex) {
|
|
SyBlob sPath;
|
|
const char *zBase;
|
|
int nBase, rc;
|
|
if(SyBlobInit(&sPath, &pCtx->pVm->sAllocator) != SXRET_OK) {
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
zBase = YamlFieldStr(pParent, "path", &nBase);
|
|
rc = SyBlobAppend(&sPath, zBase ? zBase : "", (sxu32)nBase);
|
|
if(rc == SXRET_OK && bIndex) {
|
|
rc = SyBlobAppend(&sPath, "[", 1);
|
|
if(rc == SXRET_OK) rc = YamlBlobAppendInt(&sPath, iIndex);
|
|
if(rc == SXRET_OK) rc = SyBlobAppend(&sPath, "]", 1);
|
|
} else if(rc == SXRET_OK) {
|
|
rc = SyBlobAppend(&sPath, ".", 1);
|
|
if(rc == SXRET_OK) rc = SyBlobAppend(&sPath, zKey ? zKey : "", (sxu32)nKey);
|
|
}
|
|
if(rc != SXRET_OK) {
|
|
SyBlobRelease(&sPath);
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
rc = YamlRepath(pCtx, &sPath, pChild, (int)YamlFieldInt(pParent, "depth") + 1);
|
|
SyBlobRelease(&sPath);
|
|
return rc;
|
|
}
|
|
static int YamlNodeAdd(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis;
|
|
ph7_value *pChildren;
|
|
int rc;
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_SEQUENCE, "Add requires a YamlSequenceNode");
|
|
if(rc != PH7_OK) return rc;
|
|
SXUNUSED(nArg);
|
|
pThis = ph7_context_this(pCtx);
|
|
if(apArg[0] == 0 || !ph7_value_is_object(apArg[0]) || apArg[0]->x.pOther == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0,
|
|
YamlFieldStr(pThis, "path", 0));
|
|
}
|
|
/* Node sharing is deliberate and allowed; only inserting an ancestor of
|
|
* this node into its own subtree is rejected. */
|
|
rc = YamlCheckCycle(pCtx, apArg[0], pThis, 1);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlCheckDepth(pCtx, apArg[0], (int)YamlFieldInt(pThis, "depth") + 1);
|
|
if(rc != PH7_OK) return rc;
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
if(pChildren == 0) return PH7_CORRUPT;
|
|
(void)ph7_array_add_strkey_elem(pChildren, "", apArg[0]);
|
|
/* The insert reserves an arena member and may move live slots. */
|
|
pThis = ph7_context_this(pCtx);
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
return YamlRepathChild(pCtx, pThis, apArg[0], 0, 0,
|
|
(int)ph7_array_count(pChildren) - 1, 1);
|
|
}
|
|
static int YamlNodeSet(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis;
|
|
ph7_value *pChildren;
|
|
const char *zName;
|
|
int nName = 0, rc;
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_MAPPING, "Set requires a YamlMappingNode");
|
|
if(rc != PH7_OK) return rc;
|
|
SXUNUSED(nArg);
|
|
pThis = ph7_context_this(pCtx);
|
|
if(apArg[0] && (apArg[0]->nType & MEMOBJ_OBJ) && apArg[0]->x.pOther != 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_UNSUPPORTED,
|
|
"Complex mapping keys are not supported", 0, 0,
|
|
YamlFieldStr(pThis, "path", 0));
|
|
}
|
|
/* Keep the document's scalar lexeme verbatim (quotes already removed):
|
|
* '1' and '0x1' are distinct keys. The explicit length preserves NULs, and
|
|
* '<<' is stored as an ordinary key with no merge semantics. */
|
|
zName = ph7_value_to_string(apArg[0], &nName);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
if(apArg[1] == 0 || !ph7_value_is_object(apArg[1]) || apArg[1]->x.pOther == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0,
|
|
YamlFieldStr(pThis, "path", 0));
|
|
}
|
|
rc = YamlCheckCycle(pCtx, apArg[1], pThis, 1);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlCheckDepth(pCtx, apArg[1], (int)YamlFieldInt(pThis, "depth") + 1);
|
|
if(rc != PH7_OK) return rc;
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
if(pChildren == 0) return PH7_CORRUPT;
|
|
/* Encode the key so numeric-looking lexemes never collapse onto an int
|
|
* hash key (YamlMapFetch/HasProperty/Remove decode it again). */
|
|
rc = YamlMapStore(pCtx, pChildren, zName ? zName : "", nName, apArg[1]);
|
|
if(rc != PH7_OK) return rc;
|
|
pThis = ph7_context_this(pCtx); /* reacquire after the insert */
|
|
return YamlRepathChild(pCtx, pThis, apArg[1], zName, nName, 0, 0);
|
|
}
|
|
static int YamlNodeSetIndex(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis;
|
|
ph7_value *pChildren;
|
|
ph7_int64 iIndex;
|
|
int rc;
|
|
rc = YamlNodeCheckKind(pCtx, YAML_NODE_SEQUENCE, "SetIndex requires a YamlSequenceNode");
|
|
if(rc != PH7_OK) return rc;
|
|
SXUNUSED(nArg);
|
|
pThis = ph7_context_this(pCtx);
|
|
iIndex = ph7_value_to_int64(apArg[0]);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
if(apArg[1] == 0 || !ph7_value_is_object(apArg[1]) || apArg[1]->x.pOther == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0,
|
|
YamlFieldStr(pThis, "path", 0));
|
|
}
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
{
|
|
ph7_value *pExisting = YamlArrayGet(pCtx, pChildren, iIndex);
|
|
if(pExisting) ph7_context_release_value(pCtx, pExisting);
|
|
if(pChildren == 0 || iIndex < 0 || pExisting == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_INDEX_OUT_OF_RANGE,
|
|
"Index out of range", 0, 0, YamlFieldStr(pThis, "path", 0));
|
|
}
|
|
}
|
|
rc = YamlCheckCycle(pCtx, apArg[1], pThis, 1);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlCheckDepth(pCtx, apArg[1], (int)YamlFieldInt(pThis, "depth") + 1);
|
|
if(rc != PH7_OK) return rc;
|
|
/* Cycle/depth checks may reserve arena slots and move live values. */
|
|
pChildren = YamlFieldContainer(ph7_context_this(pCtx), "children");
|
|
if(pChildren == 0) return PH7_CORRUPT;
|
|
(void)ph7_array_add_intkey_elem(pChildren, (int)iIndex, apArg[1]);
|
|
pThis = ph7_context_this(pCtx); /* reacquire after the insert */
|
|
return YamlRepathChild(pCtx, pThis, apArg[1], 0, 0, (int)iIndex, 1);
|
|
}
|
|
struct YamlRemoveWalk {
|
|
ph7_context *pCtx;
|
|
ph7_value *pOut;
|
|
const char *zName;
|
|
int nName;
|
|
ph7_int64 iIndex;
|
|
int bByIndex;
|
|
int bRemoved;
|
|
int nNext; /* Next contiguous array key after removal */
|
|
};
|
|
static int YamlRemoveWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct YamlRemoveWalk *pWalk = (struct YamlRemoveWalk *)pUserData;
|
|
int bSkip = 0;
|
|
ph7_value sTmp;
|
|
if(pWalk->bByIndex) {
|
|
bSkip = pKey && ph7_value_is_int(pKey) && ph7_value_to_int64(pKey) == pWalk->iIndex;
|
|
} else if(pKey) {
|
|
int nKeyLen = 0;
|
|
const char *zKey = YamlKeyDecode(pKey, &nKeyLen);
|
|
bSkip = zKey && nKeyLen == pWalk->nName &&
|
|
memcmp(zKey, pWalk->zName, (size_t)nKeyLen) == 0;
|
|
}
|
|
if(bSkip) {
|
|
pWalk->bRemoved = 1;
|
|
return 0;
|
|
}
|
|
PH7_MemObjInit(pWalk->pCtx->pVm, &sTmp);
|
|
(void)PH7_MemObjStore(pValue, &sTmp);
|
|
if(pWalk->bByIndex) {
|
|
/* Repack the surviving entries as 0..n-1; keeping the old keys would
|
|
* leave a hole that breaks GetIndex()/SetIndex() and auto-indexing. */
|
|
(void)ph7_array_add_intkey_elem(pWalk->pOut, pWalk->nNext++, &sTmp);
|
|
} else {
|
|
/* Keep the original key object so embedded NUL bytes survive. */
|
|
(void)ph7_array_add_elem(pWalk->pOut, pKey, &sTmp);
|
|
}
|
|
PH7_MemObjRelease(&sTmp);
|
|
return 0;
|
|
}
|
|
static int YamlNodeRemove(ph7_context *pCtx, int nArg, ph7_value **apArg, int bByIndex) {
|
|
ph7_value *pThis;
|
|
ph7_value *pChildren, *pNew;
|
|
struct YamlRemoveWalk sWalk;
|
|
const char *zName = "";
|
|
int nName = 0, rc;
|
|
rc = YamlNodeCheckKind(pCtx, bByIndex ? YAML_NODE_SEQUENCE : YAML_NODE_MAPPING,
|
|
bByIndex ? "RemoveAt requires a YamlSequenceNode" : "Remove requires a YamlMappingNode");
|
|
if(rc != PH7_OK) return rc;
|
|
SXUNUSED(nArg);
|
|
pThis = ph7_context_this(pCtx);
|
|
pChildren = YamlFieldContainer(pThis, "children");
|
|
pNew = ph7_context_new_array(pCtx);
|
|
if(pChildren == 0 || pNew == 0) {
|
|
if(pNew) ph7_context_release_value(pCtx, pNew);
|
|
return PH7_ABORT;
|
|
}
|
|
SyZero(&sWalk, sizeof(sWalk));
|
|
sWalk.pCtx = pCtx;
|
|
sWalk.pOut = pNew;
|
|
sWalk.bByIndex = bByIndex;
|
|
if(bByIndex) {
|
|
sWalk.iIndex = ph7_value_to_int64(apArg[0]);
|
|
} else {
|
|
zName = ph7_value_to_string(apArg[0], &nName);
|
|
sWalk.zName = zName;
|
|
sWalk.nName = nName;
|
|
}
|
|
if(ph7_context_status(pCtx) != PH7_OK) {
|
|
ph7_context_release_value(pCtx, pNew);
|
|
return ph7_context_status(pCtx);
|
|
}
|
|
(void)ph7_array_walk(pChildren, YamlRemoveWalker, &sWalk);
|
|
(void)YamlFieldSetObj(pThis, "children", pNew);
|
|
ph7_context_release_value(pCtx, pNew);
|
|
/* Removal changes the subtree of every survivor: full repath. */
|
|
rc = YamlRepathNode(pCtx, pThis);
|
|
if(rc != PH7_OK) return rc;
|
|
return ph7_result_bool(pCtx, sWalk.bRemoved);
|
|
}
|
|
static int YamlNodeRemoveName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return YamlNodeRemove(pCtx, nArg, apArg, 0);
|
|
}
|
|
static int YamlNodeRemoveAt(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return YamlNodeRemove(pCtx, nArg, apArg, 1);
|
|
}
|
|
static int YamlNodeClear(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
ph7_value *pNew = ph7_context_new_array(pCtx);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if(pNew == 0) return PH7_ABORT;
|
|
(void)YamlFieldSetObj(pThis, "children", pNew);
|
|
ph7_context_release_value(pCtx, pNew);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlNodeToYaml(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return YamlSerialize(pCtx, ph7_context_this(pCtx), nArg > 0 ? apArg[0] : 0);
|
|
}
|
|
static int YamlNodeToString(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return YamlSerialize(pCtx, ph7_context_this(pCtx), 0);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Method callbacks: YamlDocument / Yaml */
|
|
/* ------------------------------------------------------------------ */
|
|
/* Build a placeholder document holding a null root. The real parser will
|
|
* replace the root; this keeps every signature and the repath contract live. */
|
|
static int YamlNewDocument(ph7_context *pCtx, ph7_value **ppDoc) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pClass = PH7_VmExtractClass(pVm, YCLS_DOC, sizeof(YCLS_DOC) - 1, FALSE);
|
|
ph7_class_instance *pInst;
|
|
ph7_value *pDoc, *pRoot;
|
|
if(pClass == 0) return PH7_ABORT;
|
|
pDoc = ph7_context_new_scalar(pCtx);
|
|
if(pDoc == 0) return PH7_ABORT;
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
ph7_context_release_value(pCtx, pDoc);
|
|
PH7_VmMemoryError(pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
if(pInst->iRef < 1) pInst->iRef = 1;
|
|
PH7_MemObjRelease(pDoc);
|
|
pDoc->x.pOther = pInst;
|
|
MemObjSetType(pDoc, MEMOBJ_OBJ);
|
|
pRoot = YamlNewNode(pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
if(pRoot == 0) {
|
|
ph7_context_release_value(pCtx, pDoc);
|
|
return PH7_ABORT;
|
|
}
|
|
(void)YamlFieldSetObj(pDoc, "root", pRoot);
|
|
ph7_context_release_value(pCtx, pRoot);
|
|
*ppDoc = pDoc;
|
|
return PH7_OK;
|
|
}
|
|
static int YamlDocumentConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pRoot;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
pRoot = YamlNewNode(pCtx, YCLS_NULL, YAML_NODE_NULL_NODE, "$");
|
|
if(pRoot == 0) return PH7_ABORT;
|
|
(void)YamlFieldSetObj(ph7_context_this(pCtx), "root", pRoot);
|
|
ph7_context_release_value(pCtx, pRoot);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlDocumentParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
yaml_scanner s;
|
|
yaml_opt sOpt;
|
|
ph7_value *pRoot = 0;
|
|
const char *zText;
|
|
int nByte = 0, rc;
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
|
return YamlThrow(pCtx, YAML_ERROR_SYNTAX, "Syntax error", 0, 0, "$");
|
|
}
|
|
rc = YamlOptLoad(pCtx, nArg > 1 ? apArg[1] : 0, &sOpt);
|
|
if(rc != PH7_OK) return rc;
|
|
zText = ph7_value_to_string(apArg[0], &nByte);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
rc = YamlScanInit(&s, pCtx, zText, nByte, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
YamlScanRelease(&s);
|
|
return rc;
|
|
}
|
|
rc = YamlRunParse(&s, 0, &pRoot, 0);
|
|
YamlScanRelease(&s);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlRepathFrom(pCtx, pRoot, "$", 0);
|
|
if(rc != PH7_OK) return rc;
|
|
(void)YamlFieldSetObj(ph7_context_this(pCtx), "root", pRoot);
|
|
ph7_context_release_value(pCtx, pRoot);
|
|
return PH7_OK;
|
|
}
|
|
static int YamlDocumentGetRoot(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pRoot = YamlField(ph7_context_this(pCtx), "root");
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
return pRoot ? ph7_result_value(pCtx, pRoot) : ph7_result_null(pCtx);
|
|
}
|
|
static int YamlDocumentSetRoot(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pRoot = (nArg > 0 && ph7_value_is_object(apArg[0]) && apArg[0]->x.pOther != 0)
|
|
? apArg[0] : 0;
|
|
int rc;
|
|
if(pRoot == 0) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH, "YamlNode expected", 0, 0, "$");
|
|
}
|
|
/* A subtree that already contains itself cannot become a document root. */
|
|
rc = YamlCheckCycle(pCtx, pRoot, pRoot, 0);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlCheckDepth(pCtx, pRoot, 0);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlFieldSetObj(ph7_context_this(pCtx), "root", pRoot);
|
|
if(rc != PH7_OK) return rc;
|
|
return YamlRepathFrom(pCtx, pRoot, "$", 0);
|
|
}
|
|
static int YamlDocumentToYaml(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pRoot = YamlField(ph7_context_this(pCtx), "root");
|
|
return YamlSerialize(pCtx, pRoot, nArg > 0 ? apArg[0] : 0);
|
|
}
|
|
static int YamlParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
yaml_scanner s;
|
|
yaml_opt sOpt;
|
|
ph7_value *pRoot = 0, *pDoc = 0;
|
|
const char *zText;
|
|
int nByte = 0, rc;
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
|
return YamlThrow(pCtx, YAML_ERROR_SYNTAX, "Syntax error", 0, 0, "$");
|
|
}
|
|
rc = YamlOptLoad(pCtx, nArg > 1 ? apArg[1] : 0, &sOpt);
|
|
if(rc != PH7_OK) return rc;
|
|
zText = ph7_value_to_string(apArg[0], &nByte);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
rc = YamlScanInit(&s, pCtx, zText, nByte, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
YamlScanRelease(&s);
|
|
return rc;
|
|
}
|
|
rc = YamlRunParse(&s, 0, &pRoot, 0);
|
|
YamlScanRelease(&s);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlDocFromRoot(pCtx, pRoot, &pDoc);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = ph7_result_value(pCtx, pDoc);
|
|
ph7_context_release_value(pCtx, pDoc);
|
|
return rc;
|
|
}
|
|
static int YamlParseAll(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
yaml_scanner s;
|
|
yaml_opt sOpt;
|
|
ph7_value *pOut = 0;
|
|
const char *zText;
|
|
int nByte = 0, rc;
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
|
return YamlThrow(pCtx, YAML_ERROR_SYNTAX, "Syntax error", 0, 0, "$");
|
|
}
|
|
rc = YamlOptLoad(pCtx, nArg > 1 ? apArg[1] : 0, &sOpt);
|
|
if(rc != PH7_OK) return rc;
|
|
zText = ph7_value_to_string(apArg[0], &nByte);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
rc = YamlScanInit(&s, pCtx, zText, nByte, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
YamlScanRelease(&s);
|
|
return rc;
|
|
}
|
|
rc = YamlRunParse(&s, 1, 0, &pOut);
|
|
YamlScanRelease(&s);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = ph7_result_value(pCtx, pOut);
|
|
ph7_context_release_value(pCtx, pOut);
|
|
return rc;
|
|
}
|
|
static int YamlParseNode(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
yaml_scanner s;
|
|
yaml_opt sOpt;
|
|
ph7_value *pNode = 0;
|
|
const char *zText;
|
|
int nByte = 0, rc;
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
|
return YamlThrow(pCtx, YAML_ERROR_SYNTAX, "Syntax error", 0, 0, "$");
|
|
}
|
|
rc = YamlOptLoad(pCtx, nArg > 1 ? apArg[1] : 0, &sOpt);
|
|
if(rc != PH7_OK) return rc;
|
|
zText = ph7_value_to_string(apArg[0], &nByte);
|
|
if(ph7_context_status(pCtx) != PH7_OK) return ph7_context_status(pCtx);
|
|
rc = YamlScanInit(&s, pCtx, zText, nByte, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
YamlScanRelease(&s);
|
|
return rc;
|
|
}
|
|
rc = YamlRunParse(&s, 0, &pNode, 0);
|
|
YamlScanRelease(&s);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = YamlRepathFrom(pCtx, pNode, "$", 0);
|
|
if(rc != PH7_OK) return rc;
|
|
rc = ph7_result_value(pCtx, pNode);
|
|
ph7_context_release_value(pCtx, pNode);
|
|
return rc;
|
|
}
|
|
static int YamlSerializeFn(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return YamlSerialize(pCtx, nArg > 0 ? apArg[0] : 0, nArg > 1 ? apArg[1] : 0);
|
|
}
|
|
static int YamlSerializeAllFn(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
yaml_opt sOpt;
|
|
yaml_emit sE;
|
|
ph7_value *pDocs, *pDoc;
|
|
int rc, i, nCount;
|
|
if(nArg < 1) {
|
|
return YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH,
|
|
"Document array expected", 0, 0, "$");
|
|
}
|
|
rc = YamlOptLoad(pCtx, nArg > 1 ? apArg[1] : 0, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
rc = YamlEmitInit(&sE, pCtx, &sOpt);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
pDocs = apArg[0];
|
|
nCount = (pDocs && (pDocs->nType & MEMOBJ_HASHMAP)) ? (int)ph7_array_count(pDocs) : 0;
|
|
for(i = 0 ; i < nCount ; ++i) {
|
|
/* A separator is required before every document except the first;
|
|
* ExplicitDocumentStart additionally marks the first one. */
|
|
if(i > 0 || sOpt.explicitDocumentStart) {
|
|
rc = YamlEmitBytes(&sE, "---\n", 4);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
}
|
|
pDoc = YamlArrayGet(pCtx, pDocs, i);
|
|
if(pDoc == 0 || !ph7_value_is_object(pDoc) || pDoc->x.pOther == 0) {
|
|
if(pDoc) ph7_context_release_value(pCtx, pDoc);
|
|
rc = YamlThrow(pCtx, YAML_ERROR_TYPE_MISMATCH,
|
|
"YamlDocument expected", 0, 0, "$");
|
|
break;
|
|
}
|
|
rc = YamlEmitRoot(&sE, YamlField(pDoc, "root"));
|
|
ph7_context_release_value(pCtx, pDoc);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
if(sOpt.explicitDocumentEnd) {
|
|
rc = YamlEmitBytes(&sE, "...\n", 4);
|
|
if(rc != PH7_OK) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if(rc == PH7_OK) {
|
|
rc = ph7_result_string(pCtx, (const char *)SyBlobData(&sE.sOut),
|
|
(int)SyBlobLength(&sE.sOut));
|
|
}
|
|
YamlEmitRelease(&sE);
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Native class registration */
|
|
/* ------------------------------------------------------------------ */
|
|
static ph7_constant_def aNodeTypeConst[] = {
|
|
{ "MAPPING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "SEQUENCE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "STRING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "INT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "FLOAT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "BOOLEAN", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "NULL_NODE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
};
|
|
static const int aNodeTypeValue[] = {
|
|
YAML_NODE_MAPPING, YAML_NODE_SEQUENCE, YAML_NODE_STRING, YAML_NODE_INT,
|
|
YAML_NODE_FLOAT, YAML_NODE_BOOLEAN, YAML_NODE_NULL_NODE
|
|
};
|
|
static ph7_constant_def aExceptionConst[] = {
|
|
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_DEPTH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_SYNTAX", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_ENCODING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_TYPE_MISMATCH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_KEY_NOT_FOUND", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_INDEX_OUT_OF_RANGE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_DUPLICATE_KEY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_INVALID_OPERATION", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_INVALID_OPTION", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_READONLY_OPTION", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNDEFINED_ALIAS", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_ALIAS_LIMIT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNKNOWN_TAG", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNSUPPORTED", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_ALIAS_NOT_ALLOWED", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
};
|
|
static const int aExceptionValue[] = {
|
|
YAML_ERROR_NONE, YAML_ERROR_DEPTH, YAML_ERROR_SYNTAX, YAML_ERROR_ENCODING,
|
|
YAML_ERROR_TYPE_MISMATCH, YAML_ERROR_KEY_NOT_FOUND, YAML_ERROR_INDEX_OUT_OF_RANGE,
|
|
YAML_ERROR_DUPLICATE_KEY, YAML_ERROR_INVALID_OPERATION, YAML_ERROR_INVALID_OPTION,
|
|
YAML_ERROR_READONLY_OPTION, YAML_ERROR_UNDEFINED_ALIAS, YAML_ERROR_ALIAS_LIMIT,
|
|
YAML_ERROR_UNKNOWN_TAG, YAML_ERROR_UNSUPPORTED, YAML_ERROR_ALIAS_NOT_ALLOWED
|
|
};
|
|
static ph7_constant_def aOptionsConst[] = {
|
|
{ "KEEP_LAST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "KEEP_FIRST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "UNKNOWN_TAG_REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "UNKNOWN_TAG_KEEP", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "SCHEMA_CORE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "SCHEMA_JSON", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "SCHEMA_FAILSAFE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "STYLE_BLOCK", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "STYLE_FLOW", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
};
|
|
static const int aOptionsValue[] = {
|
|
YAML_DUP_KEEP_LAST, YAML_DUP_KEEP_FIRST, YAML_DUP_REJECT,
|
|
YAML_UNKNOWN_TAG_REJECT, YAML_UNKNOWN_TAG_KEEP,
|
|
YAML_SCHEMA_CORE, YAML_SCHEMA_JSON, YAML_SCHEMA_FAILSAFE,
|
|
YAML_STYLE_BLOCK, YAML_STYLE_FLOW
|
|
};
|
|
|
|
static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
|
|
static ph7_param_def aExcCtor[] = {
|
|
YP("message", STRING), YP("code", INT), YPN("inputLine", YTY(INT)),
|
|
YPN("inputColumn", YTY(INT)), YPN("path", YTY(STRING))
|
|
};
|
|
static const ph7_param_def aExcDesc[] = { YP("code", INT) };
|
|
static const ph7_param_def aNodeName[] = { YP("name", STRING) };
|
|
static const ph7_param_def aNodeIndex[] = { YP("index", INT) };
|
|
static const ph7_param_def aNodeSet[] = {
|
|
YP("name", STRING), YPN("node", YNM(YCLS_NODE))
|
|
};
|
|
static const ph7_param_def aNodeSetIndex[] = {
|
|
YP("index", INT), YPN("node", YNM(YCLS_NODE))
|
|
};
|
|
static const ph7_param_def aNodeAdd[] = { YPN("node", YNM(YCLS_NODE)) };
|
|
static const ph7_param_def aNodeCtorStr[] = { YP("value", STRING) };
|
|
static const ph7_param_def aNodeCtorInt[] = { YP("value", INT) };
|
|
static const ph7_param_def aNodeCtorFloat[] = { YP("value", FLOAT) };
|
|
static const ph7_param_def aNodeCtorBool[] = { YP("value", BOOL) };
|
|
static ph7_param_def aToYaml[] = { YPO("options", YNM(YCLS_OPTIONS), 0) };
|
|
static const ph7_param_def aSetBool[] = { YP("value", BOOL) };
|
|
static const ph7_param_def aSetInt[] = { YP("value", INT) };
|
|
static ph7_param_def aParse[] = {
|
|
YP("text", STRING), YPO("options", YNM(YCLS_OPTIONS), 0)
|
|
};
|
|
static ph7_param_def aSetRoot[] = { YPN("node", YNM(YCLS_NODE)) };
|
|
static ph7_param_def aSerialize[] = {
|
|
YPN("node", YNM(YCLS_NODE)), YPO("options", YNM(YCLS_OPTIONS), 0)
|
|
};
|
|
static ph7_param_def aSerializeAll[] = {
|
|
YPN("documents", YAR(YCLS_DOC)), YPO("options", YNM(YCLS_OPTIONS), 0)
|
|
};
|
|
static const ph7_method_def aExcMethod[] = {
|
|
{ "__construct", YTY(VOID), aExcCtor, 5, YamlExceptionConstruct, 0, 0 },
|
|
{ "GetInputLine", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC },
|
|
{ "GetInputColumn", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC },
|
|
{ "GetPath", YTY(STRING), 0, 0, YamlExceptionField, (void *)"path", PH7_MEMBER_PUBLIC },
|
|
{ "GetDescription", YTY(STRING), aExcDesc, 1, YamlExceptionDescription, 0,
|
|
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
|
|
};
|
|
static ph7_field_def aExcField[] = {
|
|
{ "inputLine", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "inputColumn", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
};
|
|
static const char *const aExcBase[] = { "Exception" };
|
|
static const ph7_method_def aNodeMethod[] = {
|
|
{ "GetNodeType", YTY(INT), 0, 0, YamlNodeGetIntField, (void *)"kind", PH7_MEMBER_PUBLIC },
|
|
{ "GetPath", YTY(STRING), 0, 0, YamlNodeGetStrField, (void *)"path", PH7_MEMBER_PUBLIC },
|
|
{ "GetDepth", YTY(INT), 0, 0, YamlNodeGetDepth, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetTag", YTY(STRING), 0, 0, YamlNodeGetTag, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetProperty", YNM(YCLS_NODE), aNodeName, 1, YamlNodeGetProperty, 0, PH7_MEMBER_PUBLIC },
|
|
{ "HasProperty", YTY(BOOL), aNodeName, 1, YamlNodeHasProperty, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetPropertyNames", YTYARR(STRING), 0, 0, YamlNodeGetPropertyNames, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetIndex", YNM(YCLS_NODE), aNodeIndex, 1, YamlNodeGetIndex, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetCount", YTY(INT), 0, 0, YamlNodeGetCount, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetChildren", YAR(YCLS_NODE), 0, 0, YamlNodeGetChildren, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Add", YTY(VOID), aNodeAdd, 1, YamlNodeAdd, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Set", YTY(VOID), aNodeSet, 2, YamlNodeSet, 0, PH7_MEMBER_PUBLIC },
|
|
{ "SetIndex", YTY(VOID), aNodeSetIndex, 2, YamlNodeSetIndex, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Remove", YTY(BOOL), aNodeName, 1, YamlNodeRemoveName, 0, PH7_MEMBER_PUBLIC },
|
|
{ "RemoveAt", YTY(BOOL), aNodeIndex, 1, YamlNodeRemoveAt, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Clear", YTY(VOID), 0, 0, YamlNodeClear, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetString", YTY(STRING), 0, 0, YamlNodeGetString, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetInt", YTY(INT), 0, 0, YamlNodeGetInt, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetDouble", YTY(FLOAT), 0, 0, YamlNodeGetDouble, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetBool", YTY(BOOL), 0, 0, YamlNodeGetBool, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetRawValue", YTY(STRING), 0, 0, YamlNodeGetRawValue, 0, PH7_MEMBER_PUBLIC },
|
|
{ "IsNull", YTY(BOOL), 0, 0, YamlNodeIsNull, 0, PH7_MEMBER_PUBLIC },
|
|
{ "ToYaml", YTY(STRING), aToYaml, 1, YamlNodeToYaml, 0, PH7_MEMBER_PUBLIC },
|
|
{ "__toString", YTY(STRING), 0, 0, YamlNodeToString, 0, 0 },
|
|
};
|
|
static ph7_field_def aNodeField[] = {
|
|
{ "kind", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "text", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "intValue", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "realValue", YTY(FLOAT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "boolValue", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "rawNumber", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "children", YAR(YCLS_NODE), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "depth", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "tag", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
};
|
|
#define YAML_DEPTH_FIELD_INDEX 8
|
|
static const char *const aNodeBase[] = { YCLS_NODE };
|
|
static const ph7_method_def aMappingMethod[] = {
|
|
{ "__construct", YTY(VOID), 0, 0, YamlNodeConstruct, (void *)(long)YAML_NODE_MAPPING, 0 }
|
|
};
|
|
static const ph7_method_def aSequenceMethod[] = {
|
|
{ "__construct", YTY(VOID), 0, 0, YamlNodeConstruct, (void *)(long)YAML_NODE_SEQUENCE, 0 }
|
|
};
|
|
static const ph7_method_def aStringMethod[] = {
|
|
{ "__construct", YTY(VOID), aNodeCtorStr, 1, YamlNodeCtorString, (void *)(long)YAML_NODE_STRING, 0 }
|
|
};
|
|
static const ph7_method_def aIntMethod[] = {
|
|
{ "__construct", YTY(VOID), aNodeCtorInt, 1, YamlNodeCtorInt, (void *)(long)YAML_NODE_INT, 0 }
|
|
};
|
|
static const ph7_method_def aFloatMethod[] = {
|
|
{ "__construct", YTY(VOID), aNodeCtorFloat, 1, YamlNodeCtorFloat, (void *)(long)YAML_NODE_FLOAT, 0 }
|
|
};
|
|
static const ph7_method_def aBoolMethod[] = {
|
|
{ "__construct", YTY(VOID), aNodeCtorBool, 1, YamlNodeCtorBool, (void *)(long)YAML_NODE_BOOLEAN, 0 }
|
|
};
|
|
static const ph7_method_def aNullMethod[] = {
|
|
{ "__construct", YTY(VOID), 0, 0, YamlNodeConstruct, (void *)(long)YAML_NODE_NULL_NODE, 0 }
|
|
};
|
|
static ph7_field_def aOptField[] = {
|
|
{ "allowUnicode", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "allowAliases", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "maxAliasExpansion", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "duplicateKeyPolicy", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "unknownTagPolicy", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "schema", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "explicitDocumentStart", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "explicitDocumentEnd", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "style", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "maxDepth", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "indent", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "readOnly", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
};
|
|
#define YAML_OPT_ALLOW_UNICODE 0
|
|
#define YAML_OPT_ALLOW_ALIASES 1
|
|
#define YAML_OPT_MAX_ALIAS_EXPANSION 2
|
|
#define YAML_OPT_DUPLICATE_KEY_POLICY 3
|
|
#define YAML_OPT_UNKNOWN_TAG_POLICY 4
|
|
#define YAML_OPT_SCHEMA 5
|
|
#define YAML_OPT_EXPLICIT_DOC_START 6
|
|
#define YAML_OPT_EXPLICIT_DOC_END 7
|
|
#define YAML_OPT_STYLE 8
|
|
#define YAML_OPT_MAX_DEPTH 9
|
|
#define YAML_OPT_INDENT 10
|
|
#define YAML_OPT_READ_ONLY 11
|
|
static const ph7_method_def aOptMethod[] = {
|
|
{ "__construct", YTY(VOID), 0, 0, YamlOptionsConstruct, 0, 0 },
|
|
{ "MakeReadOnly", YTY(VOID), 0, 0, YamlOptionsMakeReadOnly, 0, PH7_MEMBER_PUBLIC },
|
|
{ "IsReadOnly", YTY(BOOL), 0, 0, YamlOptionsIsReadOnly, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Validate", YTY(VOID), 0, 0, YamlOptionsValidate, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Copy", YNM(YCLS_OPTIONS), 0, 0, YamlOptionsCopy, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetAllowUnicode", YTY(BOOL), 0, 0, YamlOptionsGet, (void *)"allowUnicode", PH7_MEMBER_PUBLIC },
|
|
{ "SetAllowUnicode", YTY(VOID), aSetBool, 1, YamlOptionsSetBool, (void *)"allowUnicode", PH7_MEMBER_PUBLIC },
|
|
{ "GetAllowAliases", YTY(BOOL), 0, 0, YamlOptionsGet, (void *)"allowAliases", PH7_MEMBER_PUBLIC },
|
|
{ "SetAllowAliases", YTY(VOID), aSetBool, 1, YamlOptionsSetBool, (void *)"allowAliases", PH7_MEMBER_PUBLIC },
|
|
{ "GetMaxAliasExpansion", YTY(INT), 0, 0, YamlOptionsGet, (void *)"maxAliasExpansion", PH7_MEMBER_PUBLIC },
|
|
{ "SetMaxAliasExpansion", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"maxAliasExpansion", PH7_MEMBER_PUBLIC },
|
|
{ "GetDuplicateKeyPolicy", YTY(INT), 0, 0, YamlOptionsGet, (void *)"duplicateKeyPolicy", PH7_MEMBER_PUBLIC },
|
|
{ "SetDuplicateKeyPolicy", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"duplicateKeyPolicy", PH7_MEMBER_PUBLIC },
|
|
{ "GetUnknownTagPolicy", YTY(INT), 0, 0, YamlOptionsGet, (void *)"unknownTagPolicy", PH7_MEMBER_PUBLIC },
|
|
{ "SetUnknownTagPolicy", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"unknownTagPolicy", PH7_MEMBER_PUBLIC },
|
|
{ "GetSchema", YTY(INT), 0, 0, YamlOptionsGet, (void *)"schema", PH7_MEMBER_PUBLIC },
|
|
{ "SetSchema", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"schema", PH7_MEMBER_PUBLIC },
|
|
{ "GetExplicitDocumentStart", YTY(BOOL), 0, 0, YamlOptionsGet, (void *)"explicitDocumentStart", PH7_MEMBER_PUBLIC },
|
|
{ "SetExplicitDocumentStart", YTY(VOID), aSetBool, 1, YamlOptionsSetBool, (void *)"explicitDocumentStart", PH7_MEMBER_PUBLIC },
|
|
{ "GetExplicitDocumentEnd", YTY(BOOL), 0, 0, YamlOptionsGet, (void *)"explicitDocumentEnd", PH7_MEMBER_PUBLIC },
|
|
{ "SetExplicitDocumentEnd", YTY(VOID), aSetBool, 1, YamlOptionsSetBool, (void *)"explicitDocumentEnd", PH7_MEMBER_PUBLIC },
|
|
{ "GetStyle", YTY(INT), 0, 0, YamlOptionsGet, (void *)"style", PH7_MEMBER_PUBLIC },
|
|
{ "SetStyle", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"style", PH7_MEMBER_PUBLIC },
|
|
{ "GetMaxDepth", YTY(INT), 0, 0, YamlOptionsGet, (void *)"maxDepth", PH7_MEMBER_PUBLIC },
|
|
{ "SetMaxDepth", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"maxDepth", PH7_MEMBER_PUBLIC },
|
|
{ "GetIndent", YTY(INT), 0, 0, YamlOptionsGet, (void *)"indent", PH7_MEMBER_PUBLIC },
|
|
{ "SetIndent", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"indent", PH7_MEMBER_PUBLIC },
|
|
};
|
|
static const ph7_method_def aDocMethod[] = {
|
|
{ "__construct", YTY(VOID), 0, 0, YamlDocumentConstruct, 0, 0 },
|
|
{ "Parse", YTY(VOID), aParse, 2, YamlDocumentParse, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetRoot", YNM(YCLS_NODE), 0, 0, YamlDocumentGetRoot, 0, PH7_MEMBER_PUBLIC },
|
|
{ "SetRoot", YTY(VOID), aSetRoot, 1, YamlDocumentSetRoot, 0, PH7_MEMBER_PUBLIC },
|
|
{ "ToYaml", YTY(STRING), aToYaml, 1, YamlDocumentToYaml, 0, PH7_MEMBER_PUBLIC },
|
|
};
|
|
static ph7_field_def aDocField[] = {
|
|
{ "root", YNM(YCLS_NODE), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
};
|
|
static const ph7_method_def aFacadeMethod[] = {
|
|
{ "Parse", YNM(YCLS_DOC), aParse, 2, YamlParse, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
{ "ParseAll", YAR(YCLS_DOC), aParse, 2, YamlParseAll, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
{ "ParseNode", YNM(YCLS_NODE), aParse, 2, YamlParseNode, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
{ "Serialize", YTY(STRING), aSerialize, 2, YamlSerializeFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
{ "SerializeAll", YTY(STRING), aSerializeAll, 2, YamlSerializeAllFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
|
};
|
|
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_EXC,
|
|
aExcField, SX_ARRAYSIZE(aExcField), aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
|
|
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sNode = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_NODE,
|
|
aNodeField, SX_ARRAYSIZE(aNodeField), 0, 0, aNodeMethod, SX_ARRAYSIZE(aNodeMethod),
|
|
0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_class_def sMapping = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_MAPPING,
|
|
0, 0, 0, 0, aMappingMethod, SX_ARRAYSIZE(aMappingMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sSequence = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_SEQUENCE,
|
|
0, 0, 0, 0, aSequenceMethod, SX_ARRAYSIZE(aSequenceMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sString = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_STRING,
|
|
0, 0, 0, 0, aStringMethod, SX_ARRAYSIZE(aStringMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sInt = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_INT,
|
|
0, 0, 0, 0, aIntMethod, SX_ARRAYSIZE(aIntMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sFloat = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_FLOAT,
|
|
0, 0, 0, 0, aFloatMethod, SX_ARRAYSIZE(aFloatMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sBool = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_BOOL,
|
|
0, 0, 0, 0, aBoolMethod, SX_ARRAYSIZE(aBoolMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sNull = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_NULL,
|
|
0, 0, 0, 0, aNullMethod, SX_ARRAYSIZE(aNullMethod), 0, aNodeBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sOpt = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_OPTIONS,
|
|
aOptField, SX_ARRAYSIZE(aOptField), aOptionsConst, SX_ARRAYSIZE(aOptionsConst),
|
|
aOptMethod, SX_ARRAYSIZE(aOptMethod), 0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_class_def sDoc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_DOC,
|
|
aDocField, SX_ARRAYSIZE(aDocField), 0, 0, aDocMethod, SX_ARRAYSIZE(aDocMethod),
|
|
0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_class_def sFacade = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_FACADE,
|
|
0, 0, 0, 0, aFacadeMethod, SX_ARRAYSIZE(aFacadeMethod), 0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_class_def sKind = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_KIND,
|
|
0, 0, aNodeTypeConst, SX_ARRAYSIZE(aNodeTypeConst), 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_value *apExc[SX_ARRAYSIZE(aExceptionConst)];
|
|
ph7_value *apKind[SX_ARRAYSIZE(aNodeTypeConst)];
|
|
ph7_value *apOptConst[SX_ARRAYSIZE(aOptionsConst)];
|
|
ph7_value *apInit[SX_ARRAYSIZE(aOptField)];
|
|
ph7_value *apCtorDef[3];
|
|
ph7_value *pDepthInit = 0;
|
|
ph7_value *pDefaultOpts = 0;
|
|
ph7_class *pOptClass;
|
|
ph7_class_instance *pInst;
|
|
sxu32 n;
|
|
sxi32 rc = PH7_OK;
|
|
SyZero(apExc, sizeof(apExc));
|
|
SyZero(apKind, sizeof(apKind));
|
|
SyZero(apOptConst, sizeof(apOptConst));
|
|
SyZero(apInit, sizeof(apInit));
|
|
SyZero(apCtorDef, sizeof(apCtorDef));
|
|
/* Allocate constant and default snapshots. */
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
|
|
apExc[n] = ph7_new_scalar(pVm);
|
|
if(apExc[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
ph7_value_int(apExc[n], aExceptionValue[n]);
|
|
aExceptionConst[n].pValue = apExc[n];
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apKind) ; ++n) {
|
|
apKind[n] = ph7_new_scalar(pVm);
|
|
if(apKind[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
ph7_value_int(apKind[n], aNodeTypeValue[n]);
|
|
aNodeTypeConst[n].pValue = apKind[n];
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apOptConst) ; ++n) {
|
|
apOptConst[n] = ph7_new_scalar(pVm);
|
|
if(apOptConst[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
ph7_value_int(apOptConst[n], aOptionsValue[n]);
|
|
aOptionsConst[n].pValue = apOptConst[n];
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apInit) ; ++n) {
|
|
apInit[n] = ph7_new_scalar(pVm);
|
|
if(apInit[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
switch(n) {
|
|
case YAML_OPT_ALLOW_UNICODE:
|
|
case YAML_OPT_ALLOW_ALIASES:
|
|
ph7_value_bool(apInit[n], 1);
|
|
break;
|
|
case YAML_OPT_MAX_ALIAS_EXPANSION:
|
|
ph7_value_int(apInit[n], YAML_DEFAULT_MAX_ALIAS_EXPANSION);
|
|
break;
|
|
case YAML_OPT_DUPLICATE_KEY_POLICY:
|
|
ph7_value_int(apInit[n], YAML_DUP_REJECT);
|
|
break;
|
|
case YAML_OPT_UNKNOWN_TAG_POLICY:
|
|
ph7_value_int(apInit[n], YAML_UNKNOWN_TAG_REJECT);
|
|
break;
|
|
case YAML_OPT_SCHEMA:
|
|
ph7_value_int(apInit[n], YAML_SCHEMA_CORE);
|
|
break;
|
|
case YAML_OPT_MAX_DEPTH:
|
|
ph7_value_int(apInit[n], YAML_DEFAULT_MAX_DEPTH);
|
|
break;
|
|
case YAML_OPT_INDENT:
|
|
ph7_value_int(apInit[n], 2);
|
|
break;
|
|
case YAML_OPT_STYLE:
|
|
ph7_value_int(apInit[n], YAML_STYLE_BLOCK);
|
|
break;
|
|
default: /* explicitDocumentStart, explicitDocumentEnd, readOnly */
|
|
ph7_value_bool(apInit[n], 0);
|
|
break;
|
|
}
|
|
aOptField[n].pInitial = apInit[n];
|
|
}
|
|
pDepthInit = ph7_new_scalar(pVm);
|
|
if(pDepthInit == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
ph7_value_int(pDepthInit, 0);
|
|
aNodeField[YAML_DEPTH_FIELD_INDEX].pInitial = pDepthInit;
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apCtorDef) ; ++n) {
|
|
apCtorDef[n] = ph7_new_scalar(pVm);
|
|
if(apCtorDef[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
}
|
|
ph7_value_int(apCtorDef[0], 0);
|
|
ph7_value_int(apCtorDef[1], 0);
|
|
ph7_value_string(apCtorDef[2], "", 0);
|
|
aExcCtor[2].pDefault = apCtorDef[0];
|
|
aExcCtor[3].pDefault = apCtorDef[1];
|
|
aExcCtor[4].pDefault = apCtorDef[2];
|
|
/* YamlOptions must exist before the default parameter snapshot can be
|
|
* built, so it is registered first. The single, stable order is:
|
|
* options, exception, node type, node plus its seven subclasses, document,
|
|
* facade. */
|
|
rc = ph7_create_class(pVm, &sOpt);
|
|
if(rc != PH7_OK) goto Cleanup;
|
|
pOptClass = PH7_VmExtractClass(pVm, YCLS_OPTIONS, sizeof(YCLS_OPTIONS) - 1, FALSE);
|
|
if(pOptClass == 0) { rc = PH7_ABORT; goto Cleanup; }
|
|
pInst = PH7_NewClassInstance(pVm, pOptClass);
|
|
if(pInst == 0) { rc = PH7_NOMEM; goto Cleanup; }
|
|
if(pInst->iRef < 1) pInst->iRef = 1;
|
|
pDefaultOpts = ph7_new_scalar(pVm);
|
|
if(pDefaultOpts == 0) {
|
|
PH7_ClassInstanceUnref(pInst);
|
|
rc = PH7_NOMEM;
|
|
goto Cleanup;
|
|
}
|
|
PH7_MemObjRelease(pDefaultOpts);
|
|
pDefaultOpts->x.pOther = pInst;
|
|
MemObjSetType(pDefaultOpts, MEMOBJ_OBJ);
|
|
aToYaml[0].pDefault = pDefaultOpts;
|
|
aParse[1].pDefault = pDefaultOpts;
|
|
aSerialize[1].pDefault = pDefaultOpts;
|
|
aSerializeAll[1].pDefault = pDefaultOpts;
|
|
rc = ph7_create_class(pVm, &sExc);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sKind);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sNode);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sMapping);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sSequence);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sString);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sInt);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sFloat);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sBool);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sNull);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sDoc);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sFacade);
|
|
Cleanup:
|
|
aToYaml[0].pDefault = 0;
|
|
aParse[1].pDefault = 0;
|
|
aSerialize[1].pDefault = 0;
|
|
aSerializeAll[1].pDefault = 0;
|
|
aExcCtor[2].pDefault = 0;
|
|
aExcCtor[3].pDefault = 0;
|
|
aExcCtor[4].pDefault = 0;
|
|
aNodeField[YAML_DEPTH_FIELD_INDEX].pInitial = 0;
|
|
if(pDepthInit) {
|
|
ph7_release_value(pVm, pDepthInit);
|
|
}
|
|
if(pDefaultOpts) {
|
|
ph7_release_value(pVm, pDefaultOpts);
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apCtorDef) ; ++n) {
|
|
if(apCtorDef[n]) ph7_release_value(pVm, apCtorDef[n]);
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apInit) ; ++n) {
|
|
if(apInit[n]) {
|
|
ph7_release_value(pVm, apInit[n]);
|
|
aOptField[n].pInitial = 0;
|
|
}
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apOptConst) ; ++n) {
|
|
if(apOptConst[n]) {
|
|
ph7_release_value(pVm, apOptConst[n]);
|
|
aOptionsConst[n].pValue = 0;
|
|
}
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apKind) ; ++n) {
|
|
if(apKind[n]) {
|
|
ph7_release_value(pVm, apKind[n]);
|
|
aNodeTypeConst[n].pValue = 0;
|
|
}
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
|
|
if(apExc[n]) {
|
|
ph7_release_value(pVm, apExc[n]);
|
|
aExceptionConst[n].pValue = 0;
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
|
|
desc->zString = MODULE_DESC;
|
|
*ver = MODULE_VER;
|
|
return YamlRegisterClasses(pVm);
|
|
}
|