All Downloads are FREE. Search and download functionalities are using the official Maven repository.

team.unnamed.mocha.lexer.Cursor Maven / Gradle / Ivy

Go to download

A lightweight, fast and efficient Molang lexer, parser, interpreter and compiler for Java 8+

The newest version!
/*
 * This file is part of mocha, licensed under the MIT license
 *
 * Copyright (c) 2021-2023 Unnamed Team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package team.unnamed.mocha.lexer;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * Mutable class that tracks the position of characters
 * when performing lexical analysis
 *
 * 

Can be used to show the position of lexical errors * in a human-readable way

* * @since 3.0.0 */ public final class Cursor implements Cloneable { private int index = 0; private int line = 1; private int column = 0; public Cursor(final int line, final int column) { this.line = line; this.column = column; } public Cursor() { } public int index() { return index; } public int line() { return line; } public int column() { return column; } public void push(final int character) { index++; if (character == '\n') { // if it's a line break, // reset the column line++; column = 1; } else { column++; } } @Override public @NotNull Cursor clone() { return new Cursor(line, column); } @Override public String toString() { return "line " + line + ", column " + column; } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Cursor that = (Cursor) o; return line == that.line && column == that.column; } @Override public int hashCode() { return Objects.hash(line, column); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy