mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
parent
146928dcf5
commit
3bee6de208
3 changed files with 19 additions and 18 deletions
|
@ -94,9 +94,10 @@ will output:
|
|||
### Parse
|
||||
|
||||
```ts
|
||||
import { parseFile, parse } from "./parser.ts";
|
||||
import { parse } from "./parser.ts";
|
||||
import { readFileStrSync } from "../fs/read_file_str.ts";
|
||||
|
||||
const tomlObject = parseFile("file.toml");
|
||||
const tomlObject = parse(readFileStrSync("file.toml"));
|
||||
|
||||
const tomlString = 'foo.bar = "Deno"';
|
||||
const tomlObject22 = parse(tomlString);
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { existsSync } from "../fs/exists.ts";
|
||||
import { readFileStrSync } from "../fs/read_file_str.ts";
|
||||
import { deepAssign } from "../util/deep_assign.ts";
|
||||
import { pad } from "../strings/pad.ts";
|
||||
|
||||
|
@ -275,12 +273,12 @@ class Parser {
|
|||
_parseDeclarationName(declaration: string): string[] {
|
||||
const out = [];
|
||||
let acc = [];
|
||||
let inLitteral = false;
|
||||
let inLiteral = false;
|
||||
for (let i = 0; i < declaration.length; i++) {
|
||||
const c = declaration[i];
|
||||
switch (c) {
|
||||
case ".":
|
||||
if (!inLitteral) {
|
||||
if (!inLiteral) {
|
||||
out.push(acc.join(""));
|
||||
acc = [];
|
||||
} else {
|
||||
|
@ -288,10 +286,10 @@ class Parser {
|
|||
}
|
||||
break;
|
||||
case `"`:
|
||||
if (inLitteral) {
|
||||
inLitteral = false;
|
||||
if (inLiteral) {
|
||||
inLiteral = false;
|
||||
} else {
|
||||
inLitteral = true;
|
||||
inLiteral = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -538,11 +536,3 @@ export function parse(tomlString: string): object {
|
|||
tomlString = tomlString.replace(/\r\n/g, "\n").replace(/\\\n/g, "\n");
|
||||
return new Parser(tomlString).parse();
|
||||
}
|
||||
|
||||
export function parseFile(filePath: string): object {
|
||||
if (!existsSync(filePath)) {
|
||||
throw new Error("File not found");
|
||||
}
|
||||
const strFile = readFileStrSync(filePath);
|
||||
return parse(strFile);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { test } from "../testing/mod.ts";
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { parseFile, stringify } from "./parser.ts";
|
||||
import { existsSync } from "../fs/exists.ts";
|
||||
import { readFileStrSync } from "../fs/read_file_str.ts";
|
||||
import { parse, stringify } from "./parser.ts";
|
||||
import * as path from "../fs/path/mod.ts";
|
||||
const testFilesDir = path.resolve("toml", "testdata");
|
||||
|
||||
function parseFile(filePath: string): object {
|
||||
if (!existsSync(filePath)) {
|
||||
throw new Error(`File not found: ${filePath}`);
|
||||
}
|
||||
const strFile = readFileStrSync(filePath);
|
||||
return parse(strFile);
|
||||
}
|
||||
|
||||
test({
|
||||
name: "[TOML] Strings",
|
||||
fn(): void {
|
||||
|
|
Loading…
Reference in a new issue