<commit_before>package ingraph.cypherparser

import java.io.ByteArrayInputStream
import org.eclipse.emf.common.util.URI
import org.eclipse.xtext.resource.XtextResourceSet
import org.slizaa.neo4j.opencypher.OpenCypherStandaloneSetup
import org.slizaa.neo4j.opencypher.openCypher.Cypher

class CypherParser {

	def static Cypher parseFile(String fileName) {
		// https://typefox.io/how-and-why-use-xtext-without-the-ide
		val injector = new OpenCypherStandaloneSetup().createInjectorAndDoEMFRegistration();
		val resourceSet = injector.getInstance(XtextResourceSet);
		val filePath = "../queries/" + fileName + ".cypher"
		val cypher = resourceSet.getResource(URI.createFileURI(filePath), true).contents.get(0) as Cypher;
		return cypher
	}

	def static Cypher parseString(String queryString) {
		// https://wiki.eclipse.org/Xtext/FAQ
		val injector = new OpenCypherStandaloneSetup().createInjectorAndDoEMFRegistration();
		val resourceSet = injector.getInstance(XtextResourceSet);
		val resource = resourceSet.createResource(URI.createURI("http:/example.cypher"));
		val in = new ByteArrayInputStream(queryString.getBytes());
		resource.load(in, resourceSet.getLoadOptions());
		val cypher = resource.contents.get(0) as Cypher;
		return cypher
	}

}
<commit_msg>cypher-parser: throw Exception upon parse error in cypher. Closes #64.<commit_after>package ingraph.cypherparser

import java.io.ByteArrayInputStream
import org.eclipse.emf.common.util.URI
import org.eclipse.xtext.resource.XtextResourceSet
import org.slizaa.neo4j.opencypher.OpenCypherStandaloneSetup
import org.slizaa.neo4j.opencypher.openCypher.Cypher
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.validation.CheckMode
import org.eclipse.xtext.util.CancelIndicator
import org.eclipse.xtext.validation.Issue
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.diagnostics.Severity

class CypherParser {

  def static Cypher parseFile(String fileName) {
    // https://typefox.io/how-and-why-use-xtext-without-the-ide
    val injector = new OpenCypherStandaloneSetup().createInjectorAndDoEMFRegistration()
    val resourceSet = injector.getInstance(XtextResourceSet)
    val filePath = "../queries/" + fileName + ".cypher"
    val resource = resourceSet.getResource(URI.createFileURI(filePath), true)
    validateAndThrowError(resource)
    return resource.contents.get(0) as Cypher
  }

  def static Cypher parseString(String queryString) {
    // https://wiki.eclipse.org/Xtext/FAQ
    val injector = new OpenCypherStandaloneSetup().createInjectorAndDoEMFRegistration()
    val resourceSet = injector.getInstance(XtextResourceSet)
    val resource = resourceSet.createResource(URI.createURI("http:/example.cypher"))
    val in = new ByteArrayInputStream(queryString.getBytes())
    resource.load(in, resourceSet.getLoadOptions())
    validateAndThrowError(resource)
    return resource.contents.get(0) as Cypher
  }

  def protected static validateAndThrowError(Resource resource) {
    var seenError = false
    var String firstError = null
    val validator = (resource as XtextResource).getResourceServiceProvider().getResourceValidator()
    val issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl)
    for (Issue issue : issues) {
      if (issue.severity == Severity.ERROR && !seenError) {
        seenError = true
        firstError = issue.message
      }
      println(issue.message)
    }
    if (seenError) {
      throw new RuntimeException('''Error during cypher parse, the first error was: «firstError»''')
    }
  }
}

{'fraction_non_alphanumeric': 0.10226954328943681, 'fraction_numerical': 0.002801905295601009, 'mean_word_length': 4.833333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7489908', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1090, 'n_words': 208}