Skip to content

Commit

Permalink
Merge branch 'updateDependencies'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamvoss committed Jul 15, 2017
2 parents 9199669 + 54c8508 commit be5ad99
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 269 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "vscode-json-languageservice"]
path = vscode-json-languageservice
url = https://github.com/adamvoss/vscode-json-languageservice.git
url = https://github.com/Microsoft/vscode-json-languageservice.git
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"jsonc-parser": "^1.0.0",
"vscode-json-languageservice": "file:./vscode-json-languageservice",
"vscode-nls": "^2.0.2",
"yaml-ast-parser": "0.0.33"
"yaml-ast-parser": "0.0.34"
},
"devDependencies": {
"@types/js-yaml": "^3.5.31",
Expand Down
116 changes: 11 additions & 105 deletions src/parser/yamlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,32 +200,32 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
case Yaml.Kind.SCALAR: {
const instance = <Yaml.YAMLScalar>node;

const type = determineScalarType(instance)
const type = Yaml.determineScalarType(instance)

// The name is set either by the sequence or the mapping case.
const name = null;
const value = instance.value;

switch (type) {
case ScalarType.null: {
case Yaml.ScalarType.null: {
return new NullASTNode(parent, name, instance.startPosition, instance.endPosition);
}
case ScalarType.bool: {
return new BooleanASTNode(parent, name, parseYamlBoolean(value), node.startPosition, node.endPosition)
case Yaml.ScalarType.bool: {
return new BooleanASTNode(parent, name, Yaml.parseYamlBoolean(value), node.startPosition, node.endPosition)
}
case ScalarType.int: {
case Yaml.ScalarType.int: {
const result = new NumberASTNode(parent, name, node.startPosition, node.endPosition);
result.value = parseYamlInteger(value);
result.value = Yaml.parseYamlInteger(value);
result.isInteger = true;
return result;
}
case ScalarType.float: {
case Yaml.ScalarType.float: {
const result = new NumberASTNode(parent, name, node.startPosition, node.endPosition);
result.value = parseYamlFloat(value);
result.value = Yaml.parseYamlFloat(value);
result.isInteger = false;
return result;
}
case ScalarType.string: {
case Yaml.ScalarType.string: {
const result = new StringASTNode(parent, name, false, node.startPosition, node.endPosition);
result.value = node.value;
return result;
Expand All @@ -248,100 +248,6 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
}
}

export function parseYamlBoolean(input: string): boolean {
if (["true", "True", "TRUE"].lastIndexOf(input) >= 0) {
return true;
}
else if (["false", "False", "FALSE"].lastIndexOf(input) >= 0) {
return false;
}
throw `Invalid boolean "${input}"`
}

function safeParseYamlInteger(input: string): number {
// Use startsWith when es6 methods becomes available
if (input.lastIndexOf('0o', 0) === 0) {
return parseInt(input.substring(2), 8)
}

return parseInt(input);
}

export function parseYamlInteger(input: string): number {
const result = safeParseYamlInteger(input)

if (isNaN(result)) {
throw `Invalid integer "${input}"`
}

return result;
}

export function parseYamlFloat(input: string): number {

if ([".nan", ".NaN", ".NAN"].lastIndexOf(input) >= 0) {
return NaN;
}

const infinity = /^([-+])?(?:\.inf|\.Inf|\.INF)$/
const match = infinity.exec(input)
if (match) {
return (match[1] === '-') ? -Infinity : Infinity;
}

const result = parseFloat(input)

if (!isNaN(result)) {
return result;
}

throw `Invalid float "${input}"`
}

export enum ScalarType {
null, bool, int, float, string
}

export function determineScalarType(node: Yaml.YAMLScalar): ScalarType {
if (node === undefined) {
return ScalarType.null;
}

if (node.doubleQuoted || !node.plainScalar || node['singleQuoted']) {
return ScalarType.string
}

const value = node.value;

if (["null", "Null", "NULL", "~", ''].indexOf(value) >= 0) {
return ScalarType.null;
}

if (value === null || value === undefined) {
return ScalarType.null;
}

if (["true", "True", "TRUE", "false", "False", "FALSE"].indexOf(value) >= 0) {
return ScalarType.bool;
}

const base10 = /^[-+]?[0-9]+$/
const base8 = /^0o[0-7]+$/
const base16 = /^0x[0-9a-fA-F]+$/

if (base10.test(value) || base8.test(value) || base16.test(value)) {
return ScalarType.int;
}

const float = /^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$/
const infinity = /^[-+]?(\.inf|\.Inf|\.INF)$/
if (float.test(value) || infinity.test(value) || [".nan", ".NaN", ".NAN"].indexOf(value) >= 0) {
return ScalarType.float;
}

return ScalarType.string;
}

function convertError(e: Yaml.YAMLException) {
// Subtract 2 because \n\0 is added by the parser (see loader.ts/loadDocuments)
const bufferLength = e.mark.buffer.length - 2;
Expand All @@ -367,8 +273,8 @@ export function parse(text: string): JSONDocument {

const duplicateKeyReason = 'duplicate key'

const errors = yamlDoc.errors.filter(e => e.reason !== duplicateKeyReason).map(e => convertError(e))
const warnings = yamlDoc.errors.filter(e => e.reason === duplicateKeyReason).map(e => convertError(e))
const errors = yamlDoc.errors.filter(e => e.reason !== duplicateKeyReason && !e.isWarning).map(e => convertError(e))
const warnings = yamlDoc.errors.filter(e => e.reason === duplicateKeyReason || e.isWarning).map(e => convertError(e))

errors.forEach(e => _doc.errors.push(e));
warnings.forEach(e => _doc.warnings.push(e));
Expand Down
161 changes: 0 additions & 161 deletions src/test/determineScalarType.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion vscode-json-languageservice

0 comments on commit be5ad99

Please sign in to comment.