<commit_before>package org.eclipse.xtext.parseerrorhandling;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.m2t.type.emf.EmfRegistryMetaModel;
import org.eclipse.xtext.XtextGrammarTest;
import org.eclipse.xtext.XtextGrammarTestConstants;
import org.eclipse.xtext.core.parser.IParseErrorHandler;
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
import org.eclipse.xtext.parser.XtextGrammarTestASTFactory;
import org.openarchitectureware.expression.ExecutionContextImpl;
import org.openarchitectureware.xtend.XtendFacade;

public class ParseErrorHandlingTest extends AbstractGeneratorTest {

	public void testLexError() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		parse("import 'holla' % as foo", errors);
		assertEquals("%", errors.get(0).text);
		assertEquals(1, errors.get(0).line);
		assertEquals(15, errors.get(0).offset);
		assertEquals(1, errors.get(0).length);
		assertEquals(1, errors.size());
	}

	public void testParseError1() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		parse("import 'holla' foo returns x::y::Z : name=ID;", errors);
		assertEquals("::", errors.get(0).text);
		assertEquals(1, errors.get(0).line);
		assertEquals(31, errors.get(0).offset);
		assertEquals(2, errors.get(0).length);
		assertEquals(1, errors.size());
	}
	
	public void testParseError2() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID #;", new XtextGrammarTestASTFactory(), errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
	}
	
	public void testParseError3() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID #############", new XtextGrammarTestASTFactory(), errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
	}
	
	public void testParseError4() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID #############; bar : 'stuff'", new XtextGrammarTestASTFactory(), errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
		assertWithXtend("\"'stuff'\"", "parserRules.get(1).eAllContents.typeSelect(XtextTest::Keyword).first().value", object);
	}

	private final class ErrorHandler implements IParseErrorHandler {
		private final List<ErrorEntry> errors = new ArrayList<ErrorEntry>();


		public void clear() {
			errors.clear();
		}


		public ErrorEntry get(int arg0) {
			return errors.get(arg0);
		}


		public Iterator<ErrorEntry> iterator() {
			return errors.iterator();
		}


		public int size() {
			return errors.size();
		}


		public void handleParserError(int line, int offset, int length, int token, String text, String message,
				Object context) {
			errors.add(new ErrorEntry(line, offset, length, token, text, message, context));
		}
	}

	private final class ErrorEntry {
		int line;
		int offset;
		int length;
		int token;
		String text;
		String message;
		Object context;
		public ErrorEntry(int line, int offset, int length, int token, String text, String message, Object context) {
			super();
			this.line = line;
			this.offset = offset;
			this.length = length;
			this.token = token;
			this.text = text;
			this.message = message;
			this.context = context;
		}
		
		
		public int getLine() {
			return line;
		}


		public int getOffset() {
			return offset;
		}<commit_msg>*** empty log message ***<commit_after>package org.eclipse.xtext.parseerrorhandling;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.m2t.type.emf.EmfRegistryMetaModel;
import org.eclipse.xtext.XtextGrammarTest;
import org.eclipse.xtext.XtextGrammarTestConstants;
import org.eclipse.xtext.core.parser.IParseErrorHandler;
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
import org.eclipse.xtext.parser.XtextGrammarTestASTFactory;
import org.openarchitectureware.expression.ExecutionContextImpl;
import org.openarchitectureware.xtend.XtendFacade;

public class ParseErrorHandlingTest extends AbstractGeneratorTest {

	public void testLexError() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		parse("import 'holla' % as foo", errors);
		assertEquals("%", errors.get(0).text);
		assertEquals(1, errors.get(0).line);
		assertEquals(15, errors.get(0).offset);
		assertEquals(1, errors.get(0).length);
		assertEquals(1, errors.size());
	}

	public void testParseError1() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		parse("import 'holla' foo returns x::y::Z : name=ID;", errors);
		assertEquals("::", errors.get(0).text);
		assertEquals(1, errors.get(0).line);
		assertEquals(31, errors.get(0).offset);
		assertEquals(2, errors.get(0).length);
		assertEquals(1, errors.size());
	}
	
	public void testParseError2() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID #;", new XtextGrammarTestASTFactory(), errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
	}
	
	public void testParseError3() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID #############", new XtextGrammarTestASTFactory(), errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
	}
	
	public void testParseError4() throws Exception {
		ErrorHandler errors = new ErrorHandler();
		Object object = parse("import 'holla' foo returns y::Z : name=ID # 'foo'; bar : 'stuff'", new XtextGrammarTestASTFactory(), errors);
		System.out.println(errors);
		assertWithXtend("'ID'", "parserRules.first().eAllContents.typeSelect(XtextTest::RuleCall).first().name", object);
		assertWithXtend("null", "parserRules.first().eAllContents.typeSelect(XtextTest::Keyword).first().name", object);
		assertWithXtend("\"'stuff'\"", "parserRules.get(1).eAllContents.typeSelect(XtextTest::Keyword).first().value", object);
	}

	private final class ErrorHandler implements IParseErrorHandler {
		private final List<ErrorEntry> errors = new ArrayList<ErrorEntry>();


		public void clear() {
			errors.clear();
		}


		public ErrorEntry get(int arg0) {
			return errors.get(arg0);
		}


		public Iterator<ErrorEntry> iterator() {
			return errors.iterator();
		}


		public int size() {
			return errors.size();
		}


		public void handleParserError(int line, int offset, int length, int token, String text, String message,
				Object context) {
			errors.add(new ErrorEntry(line, offset, length, token, text, message, context));
		}
		
		@Override
		public String toString() {
			StringBuffer buff = new StringBuffer();
			for (ErrorEntry e : errors) {
				buff.append(e.toString()).append("\n");
			}
			return buff.toString();
		}
	}

	private final class ErrorEntry {
		int line;
		int offset;
		int length;
		int token;
		String text;
		String message;
		Object context;
		public ErrorEntry(int line, int offset, int length, int token, String text, String message, Object context) {
			super();
			this.line = line;
			this.offset = offset;
			this.length = length;
			this.token = token;
			this.text = text;
			this.message = message;
			this.context = context;
		}
		
		
		public int getLine() {
			return line;
		}


		public int getOffset() {
			return offset;
		}
{'fraction_non_alphanumeric': 0.1380467036511418, 'fraction_numerical': 0.006708811766223713, 'mean_word_length': 5.0186335403726705, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '265481', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2365, 'n_words': 562}