UNPKG

generator-jhipster

Version:

Spring Boot + Angular/React/Vue in one handy generator

63 lines (62 loc) 2.94 kB
/** * Copyright 2013-2026 the original author or authors from the JHipster project. * * This file is part of the JHipster project, see https://www.jhipster.tech/ * for more information. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { EOF } from 'chevrotain'; import { buildJDLAstBuilderVisitor } from "./jdl-ast-builder-visitor.js"; import performAdditionalSyntaxChecks from "./validator.js"; export function parse(input, runtime, options) { const cst = getCst(input, runtime, options); const astBuilderVisitor = buildJDLAstBuilderVisitor(runtime); return astBuilderVisitor.visit(cst); } export function getCst(input, runtime, options) { const lexResult = runtime.lexer.tokenize(input); if (lexResult.errors.length > 0) { throw new Error(lexResult.errors[0].message); } runtime.parser.input = lexResult.tokens; const cst = runtime.parser[options?.startRule ?? 'prog'](); if (runtime.parser.errors.length > 0) { throwParserError(runtime.parser.errors); } const extraSyntaxErrors = performAdditionalSyntaxChecks(cst, runtime); if (extraSyntaxErrors.length > 0) { throwSyntaxError(extraSyntaxErrors); } return cst; } function throwParserError(errors) { const parserError = errors[0]; if (parserError.name === 'MismatchedTokenException') { throwErrorAboutInvalidToken(parserError); } const errorMessage = `${parserError.name}: ${parserError.message}`; const { token } = parserError; const errorMessageLocation = token.tokenType === EOF ? '' : `\n\tat line: ${token.startLine}, column: ${token.startColumn}`; throw new Error(`${errorMessage}${errorMessageLocation}`); } function throwErrorAboutInvalidToken(parserError) { const { token } = parserError; const errorMessageBeginning = `Found an invalid token '${token.image}'`; const errorMessageLocation = token.tokenType === EOF ? '' : `, at line: ${token.startLine} and column: ${token.startColumn}`; const errorMessageComplement = 'Please make sure your JDL content does not use invalid characters, keywords or options.'; throw new Error(`${parserError.name}: ${errorMessageBeginning}${errorMessageLocation}.\n\t${errorMessageComplement}`); } function throwSyntaxError(errors) { throw new Error(errors.map(error => `${error.message}\n\tat line: ${error.token.startLine}, column: ${error.token.startColumn}`).join('\n')); }