@homedev/hcl 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ export declare function hasObjectBlankLine(object: HclObject, key: string): boolean;
2
+
3
+ export declare interface HclAttribute {
4
+ kind: 'attribute';
5
+ name: string;
6
+ value: HclValue;
7
+ leadingBlankLine?: boolean;
8
+ trailingComment?: string;
9
+ }
10
+
11
+ export declare interface HclBlock {
12
+ kind: 'block';
13
+ type: string;
14
+ labels: string[];
15
+ body: HclBodyItem[];
16
+ leadingBlankLine?: boolean;
17
+ trailingComment?: string;
18
+ }
19
+
20
+ export declare type HclBodyItem = HclAttribute | HclBlock | HclComment;
21
+
22
+ export declare interface HclComment {
23
+ kind: 'comment';
24
+ value: string;
25
+ leadingBlankLine?: boolean;
26
+ }
27
+
28
+ export declare interface HclDocument {
29
+ body: HclBodyItem[];
30
+ }
31
+
32
+ export declare interface HclFunction {
33
+ kind: 'function';
34
+ name: string;
35
+ arguments: HclValue[];
36
+ }
37
+
38
+ export declare interface HclObject {
39
+ [key: string]: HclValue;
40
+ }
41
+
42
+ export declare type HclScalar = string | number | boolean | null;
43
+
44
+ export declare interface HclTraversal {
45
+ kind: 'traversal';
46
+ parts: string[];
47
+ }
48
+
49
+ export declare type HclValue = HclScalar | HclFunction | HclTraversal | HclValue[] | HclObject;
50
+
51
+ export declare function markObjectBlankLine(object: HclObject, key: string): void;
52
+
53
+ export declare function parse(source: string): HclDocument;
54
+
55
+ export declare function stringify(document: HclDocument, options?: StringifyOptions): string;
56
+
57
+ export declare interface StringifyOptions {
58
+ indentation?: string;
59
+ align?: boolean;
60
+ }
61
+
62
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ var m=new WeakMap;function g(e,t){let i=m.get(e);if(!i)i=new Set,m.set(e,i);i.add(t)}function v(e,t){return m.get(e)?.has(t)??!1}function V(e){return new y(e).parseDocument()}class y{source;position=0;line=1;column=1;constructor(e){this.source=e}parseDocument(){let e=this.parseBody(!1);if(this.skipTrivia(),!this.atEnd())this.fail("Expected an attribute or block.");return{body:e}}parseBody(e){let t=[];while(!0){let i=this.skipTrivia(),n=i.blankLine;if(i.comments.forEach((o,u)=>{let a=t.at(-1);if(o.inline&&a&&a.kind!=="comment"&&!o.value.includes(`
2
+ `)){a.trailingComment=o.value;return}t.push({kind:"comment",value:o.value,...n&&u===0?{leadingBlankLine:n}:{}})}),e&&this.peek()==="}")return this.advance(),t;if(this.atEnd()){if(e)this.fail("Expected closing `}`.");return t}let r=this.readIdentifier();if(this.skipTrivia(),this.peek()==="="){this.advance(),t.push({kind:"attribute",name:r,value:this.parseValue(),...n?{leadingBlankLine:n}:{}});continue}let s=[];while(this.peek()!=="{"){if(this.atEnd())this.fail("Expected `=` or `{` after item name.");s.push(this.parseLabel()),this.skipTrivia()}this.advance(),t.push({kind:"block",type:r,labels:s,body:this.parseBody(!0),...n?{leadingBlankLine:n}:{}})}}parseLabel(){return this.skipTrivia(),this.peek()==='"'?this.readString():this.readIdentifier()}parseValue(){if(this.skipTrivia(),this.peek()==='"')return this.readString();if(this.peek()==="[")return this.parseArray();if(this.peek()==="{")return this.parseObject();if(this.readKeyword("true"))return!0;if(this.readKeyword("false"))return!1;if(this.readKeyword("null"))return null;let e=this.readNumber();if(e!==void 0)return e;let t=this.readIdentifier();if(this.skipTrivia(),this.peek()==="(")return this.parseFunction(t);if(this.peek()===".")return this.parseTraversal(t);this.fail("Expected a string, number, boolean, null, array, or object value.")}parseFunction(e){this.advance();let t=[];while(!0){if(this.skipTrivia(),this.peek()===")")return this.advance(),{kind:"function",name:e,arguments:t};if(t.push(this.parseValue()),this.skipTrivia(),this.peek()===",")this.advance();else if(this.peek()!==")")this.fail("Expected `,` or `)` in function call.")}}parseTraversal(e){let t=[e];while(this.peek()===".")this.advance(),this.skipTrivia(),t.push(this.readIdentifier()),this.skipTrivia();return{kind:"traversal",parts:t}}parseArray(){this.advance();let e=[];while(!0){if(this.skipTrivia(),this.peek()==="]")return this.advance(),e;e.push(this.parseValue());let t=this.skipTrivia();if(this.peek()===",")this.advance();else if(this.peek()!=="]"&&t.newlines===0)this.fail("Expected `,` or a new line in array.")}}parseObject(){this.advance();let e={},t=!1;while(!0){let i=this.skipTrivia();if(t||=i.blankLine,this.peek()==="}")return this.advance(),e;let n=this.peek()==='"'?this.readString():this.readIdentifier();if(t)g(e,n);if(this.skipTrivia(),this.peek()!=="="&&this.peek()!==":")this.fail("Expected `=` or `:` in object.");this.advance(),e[n]=this.parseValue();let r=this.skipTrivia();if(t=r.blankLine,this.peek()===",")this.advance();else if(this.peek()!=="}"&&r.newlines===0)this.fail("Expected `,` or a new line in object.")}}readKeyword(e){if(!this.source.startsWith(e,this.position)||H(this.source[this.position+e.length]))return!1;return this.advance(e.length),!0}readIdentifier(){if(!O(this.peek()))this.fail("Expected an identifier.");let e=this.position;this.advance();while(H(this.peek()))this.advance();return this.source.slice(e,this.position)}readNumber(){let e=/^[+-]?(?:\d+\.\d*|\.\d+|\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));if(!e)return;let t=Number(e[0]);if(!Number.isFinite(t))this.fail("Number must be finite.");return this.advance(e[0].length),t}readString(){this.advance();let e="",t={'"':'"',"\\":"\\",n:`
3
+ `,r:"\r",t:"\t"};while(!this.atEnd()&&this.peek()!=='"'){if(this.peek()===`
4
+ `||this.peek()==="\r")this.fail("Unterminated string.");if(this.peek()!=="\\"){e+=this.peek(),this.advance();continue}this.advance();let i=this.peek();if(!(i in t))this.fail(`Unsupported escape sequence \\${i}.`);e+=t[i],this.advance()}if(this.atEnd())this.fail("Unterminated string.");return this.advance(),e}skipTrivia(){let e=0,t=!1,i=!0,n=[];while(!this.atEnd())if(/\s/.test(this.peek())){if(this.peek()===`
5
+ `)e++,t||=!i,i=!1;this.advance()}else if(this.source.startsWith("#",this.position)||this.source.startsWith("//",this.position)){let r=this.peek()==="#"?1:2,s=this.position+r;i=!0;while(!this.atEnd()&&this.peek()!==`
6
+ `)this.advance();n.push({value:this.source.slice(s,this.position).trim(),inline:e===0})}else if(this.source.startsWith("/*",this.position)){let r=this.source.indexOf("*/",this.position+2);if(r<0)this.fail("Unterminated block comment.");i=!0,n.push({value:this.source.slice(this.position+2,r).trim(),inline:e===0}),this.advance(r+2-this.position)}else return{newlines:e,blankLine:t,comments:n};return{newlines:e,blankLine:t,comments:n}}peek(){return this.source[this.position]??""}atEnd(){return this.position>=this.source.length}advance(e=1){for(let t=0;t<e;t++){if(this.peek()===`
7
+ `)this.line++,this.column=1;else this.column++;this.position++}}fail(e){throw SyntaxError(`${e} (line ${String(this.line)}, column ${String(this.column)})`)}}function O(e){return/[A-Za-z_]/.test(e)}function H(e){return Boolean(e&&/[A-Za-z0-9_-]/.test(e))}function X(e,t={}){let{indentation:i=" ",align:n=!0}=t;if(!i)throw Error("The indentation string must not be empty.");let r=E(e.body,i,n,0);return r.length?`${r.join(`
8
+ `)}
9
+ `:""}function E(e,t,i,n){let r=t.repeat(n),s=[],o=[],u=()=>{I(o).forEach((l,h)=>{if(h>0)s.push("");let d=i?Math.max(...l.map((c)=>c.name.length)):0;l.forEach((c,p)=>{if(p>0&&B(l[p-1].value))s.push("");s.push(`${r}${c.name.padEnd(d)} = ${f(c.value,t,i,n)}${x(c.trailingComment)}`)})}),o=[]};return e.forEach((a,l)=>{if(a.kind==="attribute"){o.push(a);return}if(u(),a.kind==="comment"){if(a.leadingBlankLine)s.push("");s.push(...a.value.split(/\r?\n/).map((h)=>`${r}#${h?` ${h.trim()}`:""}`));return}if(l>0)s.push("");s.push(`${r}${[a.type,...a.labels.map(k)].join(" ")} {`),s.push(...E(a.body,t,i,n+1)),s.push(`${r}}${x(a.trailingComment)}`)}),u(),s}function f(e,t,i,n){if(typeof e==="string")return k(e);if(typeof e==="number"||typeof e==="boolean")return String(e);if(e===null)return"null";if(j(e))return`${e.name}(${e.arguments.map((l)=>f(l,t,i,n)).join(", ")})`;if(T(e))return e.parts.join(".");if(Array.isArray(e))return L(e,t,i,n);let r=Object.entries(e);if(!r.length)return"{}";let s=t.repeat(n+1),o=t.repeat(n),u=r.map(([l])=>S(l)),a=[];return A(e,r).forEach((l,h)=>{if(h>0)a.push("");let d=i?Math.max(...l.map(({index:c})=>u[c].length)):0;l.forEach(({entry:[c,p],index:w},b)=>{if(b>0&&B(l[b-1].entry[1]))a.push("");a.push(`${s}${u[w].padEnd(d)} = ${f(p,t,i,n+1)}`)})}),`{
10
+ ${a.join(`
11
+ `)}
12
+ ${o}}`}function L(e,t,i,n){if(!e.length)return"[]";let r=t.repeat(n+1),s=t.repeat(n);return`[
13
+ ${e.map((o)=>`${r}${f(o,t,i,n+1)}`).join(`
14
+ `)}
15
+ ${s}]`}function I(e){return e.reduce((t,i)=>{if(i.leadingBlankLine||!t.length)t.push([]);return t.at(-1)?.push(i),t},[])}function A(e,t){return t.reduce((i,n,r)=>{if(v(e,n[0])||!i.length)i.push([]);return i.at(-1)?.push({entry:n,index:r}),i},[])}function S(e){return F(e)?e:k(e)}function F(e){return/^[A-Za-z_][A-Za-z0-9_-]*$/.test(e)}function B(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&!j(e)&&!T(e)}function j(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="function"}function T(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)&&e.kind==="traversal"}function x(e){return e?` # ${e}`:""}function k(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}export{v as hasObjectBlankLine,g as markObjectBlankLine,V as parse,X as stringify};
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@homedev/hcl",
3
+ "version": "0.0.1",
4
+ "description": "hcl parser and serializer",
5
+ "author": "julzor",
6
+ "license": "ISC",
7
+ "main": "dist/index.js",
8
+ "type": "module",
9
+ "typings": "dist/index.d.ts"
10
+ }