Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab3.main;
- import lab2.util.variantNative.Code;
- import lab3.util.Error;
- import java.util.List;
- import java.util.Stack;
- import static javafx.scene.input.KeyCode.CLOSE_BRACKET;
- public class GermanResolverAutoLevak {
- protected List<Integer> codes;
- protected int codeNumber = 0;
- protected Stack<Integer> errors = new Stack<>();
- private boolean logicCondision;
- public GermanResolverAutoLevak() {
- }
- public void setCodes(List<Integer> codes) {
- this.codes = codes;
- codes.add(-1);
- }
- public boolean check() {
- reset();
- try {
- return isFragment();
- } catch (Throwable e) {
- return false;
- }
- }
- public Stack<Integer> getErrors() {
- return errors;
- }
- protected boolean isFragment() {
- if (!isIf()) {
- errors.push(Error.Fragment.BEGIN_WITH_IF);
- throw new IllegalArgumentException();
- }
- if (!isLogicCondition()) {
- errors.push(Error.Fragment.LOGIC_CONDITION_AFTER_IF);
- throw new IllegalArgumentException();
- }
- if (!isThen()) {
- errors.push(Error.Fragment.THEN_AFTER_LOGIC_CONDITION);
- throw new IllegalArgumentException();
- }
- if (!isOperator()) {
- errors.push(Error.Fragment.OPERATOR);
- throw new IllegalArgumentException();
- }
- errors.clear();
- return true;
- }
- public boolean isLogicCondition() {
- if (!isEquallyCondition()) {
- errors.push(Error.Expression.BEGIN_WITH_EQUALLY_CONDITION);
- return false;
- }
- while (true) {
- if (!isSign()) {
- codeNumber--;
- errors.push(Error.Expression.SIGN_AFTER_EQUALLY_CONDITION);
- return true;
- } else {
- if (!isEquallyCondition()) {
- errors.push(Error.Expression.EQUALLY_CONDITION_AFTER_SIGN);
- throw new IllegalArgumentException();
- } else {
- errors.clear();
- }
- }
- }
- }
- public boolean isEquallyCondition() {
- if (!isOpenBracket()) {
- errors.push(Error.EquallyCondition.BEGIN_WITH_OPEN_BRACKET);
- throw new IllegalArgumentException();
- }
- if (!isOperand()) {
- errors.push(Error.EquallyCondition.OPERAND_AFTER_OPEN_BRACKET);
- throw new IllegalArgumentException();
- }
- if (!isDoubleEqually()) {
- errors.push(Error.EquallyCondition.DOUBLE_EQUALLY_AFTER_OPERAND);
- throw new IllegalArgumentException();
- }
- if (!isOperand()) {
- errors.push(Error.EquallyCondition.OPERAND_AFTER_CLOSE_BRACKET);
- throw new IllegalArgumentException();
- }
- if (!isCloseBracket()) {
- errors.push(Error.EquallyCondition.CLOSE_BRACKET_AFTER_OPERAND);
- throw new IllegalArgumentException();
- }
- errors.clear();
- return true;
- }
- protected boolean isOperator() {
- if (!isIden()) {
- errors.push(Error.Operator.BEGIN_WITH_IDEN);
- return false;
- }
- if (!isEqually()) {
- errors.push(Error.Operator.EQUALLY_AFTER_IDEN);
- throw new IllegalArgumentException();
- }
- if (!isExpression()) {
- errors.push(Error.Operator.EXPRESSION);
- return false;
- }
- errors.clear();
- return true;
- }
- protected boolean isExpression() {
- if (!isOperand()) {
- errors.push(Error.Expression.BEGIN_WITH_OPERAND);
- // throw new IllegalArgumentException();
- return false;
- }
- while (true) {
- if (!isSign()) {
- codeNumber--;
- errors.push(Error.Expression.SIGN_AFTER_OPERAND);
- return true;
- } else {
- if (!isOperand()) {
- errors.push(Error.Expression.OPERAND_AFTER_SIGN);
- throw new IllegalArgumentException();
- } else {
- errors.clear();
- }
- }
- }
- }
- protected boolean isOperand() {
- switch (codes.get(codeNumber++)) {
- case Code.IDEN:
- case Code.DATA:
- return true;
- default:
- errors.push(Error.OPERAND);
- return false;
- }
- }
- protected boolean isSign() {
- switch (codes.get(codeNumber++)) {
- case Code.Sign.PLUS:
- case Code.Sign.MINUS:
- return true;
- default:
- errors.push(Error.SIGN);
- return false;
- }
- }
- protected boolean isIf() {
- return codes.get(codeNumber++) == Code.IF;
- }
- protected boolean isThen() {
- return codes.get(codeNumber++) == Code.THEN;
- }
- protected boolean isIden() {
- return codes.get(codeNumber++) == Code.IDEN;
- }
- protected boolean isEqually() {
- return codes.get(codeNumber++) == Code.Sign.EQUALLY;
- }
- protected boolean isDoubleEqually() {
- return codes.get(codeNumber++) == Code.Sign.DOUBLE_EQUALLY;
- }
- public boolean isOpenBracket() {
- return codes.get(codeNumber++) == Code.OPEN_BRACKET;
- }
- public boolean isCloseBracket() {
- return codes.get(codeNumber++) == Code.CLOSE_BRACKET;
- }
- public void reset() {
- codeNumber = 0;
- errors.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement