From 883dbe437c0c81888cfc1d264885aed884639468 Mon Sep 17 00:00:00 2001 From: BorysLevytskyi Date: Fri, 5 May 2023 14:47:00 +0200 Subject: [PATCH] Fix bug --- package.json | 1 + src/expression/NumericOperand.ts | 13 ++++++++++++- src/expression/expression.test.ts | 16 ++++++++++++++++ src/expression/expression.ts | 24 +++++++++++------------- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index d8e8a2c..da9b1ba 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "start": "react-scripts start", "build": "react-scripts build & npx http-server ./build -p 3030", "release": "react-scripts build & gh-pages -d build --message Release", + "deploy": "gh-pages -d build --message Release", "test": "react-scripts test", "eject": "react-scripts eject", "serve-build": "npx http-server ./build -p 3030" diff --git a/src/expression/NumericOperand.ts b/src/expression/NumericOperand.ts index 3a6ee8c..3faf28e 100644 --- a/src/expression/NumericOperand.ts +++ b/src/expression/NumericOperand.ts @@ -94,10 +94,21 @@ export default class NumericOperand implements ExpressionInputItem { static parse(input: string) : NumericOperand { + var parsed = NumericOperand.tryParse(input); + + if(parsed == null) { + throw new Error(input + " is not a valid number"); + } + + return parsed; + } + + static tryParse(input: string) : NumericOperand | null { + var parsed = numberParser.parse(input); if(!parsed) { - throw new Error(input + " is not a valid number"); + return null; } return new NumericOperand(parsed.value, parsed.base); diff --git a/src/expression/expression.test.ts b/src/expression/expression.test.ts index 39f2be9..206a58d 100644 --- a/src/expression/expression.test.ts +++ b/src/expression/expression.test.ts @@ -8,12 +8,28 @@ describe("expression parser", () => { expect(result).toBeInstanceOf(ListOfNumbersExpression); }); + it("doesn't list of numbers in case of bad numbers", () => { + expect(parser.parse("1 2 z")).toBeNull(); + //expect(parser.parse("-")).toBeNull(); + expect(parser.parse("")).toBeNull(); + }); + it("pares different operations expressions", () => { expect(parser.parse("~1")).toBeInstanceOf(BitwiseOperationExpression); expect(parser.parse("1^2")).toBeInstanceOf(BitwiseOperationExpression); expect(parser.parse("1|2")).toBeInstanceOf(BitwiseOperationExpression); }); + it("parses big binary bitwise expression", () => { + const input = "0b00010010001101000101011001111000 | 0b10101010101010101010101000000000"; + const actual = parser.parse(input); + expect(actual).toBeInstanceOf(BitwiseOperationExpression); + + const expr = actual as BitwiseOperationExpression; + expect(expr.expressionItems[0].getUnderlyingOperand().value).toBe(305419896); + expect(expr.expressionItems[1].getUnderlyingOperand().value).toBe(2863311360); + }) + it("pares multiple operand expression", () => { const result = parser.parse("1^2") as BitwiseOperationExpression; expect(result.expressionItems.length).toBe(2); diff --git a/src/expression/expression.ts b/src/expression/expression.ts index d2b2fa2..dc3ceaf 100644 --- a/src/expression/expression.ts +++ b/src/expression/expression.ts @@ -61,26 +61,24 @@ class ExpressionParser { class ListOfNumbersExpressionFactory implements IExpressionParserFactory { - regex: RegExp; - constructor() { - this.regex = /^(-?(?:\d+|0x[\d,a-f]+|0b[0-1])\s?)+$/; } canCreate (input: string): boolean { - return this.regex.test(input); + if(input.length == 0) return false; + + return input.split(' ') + .filter(p => p.length > 0) + .map(p => NumericOperand.tryParse(p)) + .filter(n => n == null) + .length == 0; }; create (input : string) : ExpressionInput { - var matches = this.regex.exec(input) as RegExpExecArray; - var numbers = [] as NumericOperand[]; - var input = matches.input; - - input.split(' ').forEach((n: string) => { - if(n.trim().length > 0) { - numbers.push(NumericOperand.parse(n.trim())); - } - }); + + const numbers = input.split(' ') + .filter(p => p.length > 0) + .map(m => NumericOperand.parse(m)); return new ListOfNumbersExpression(input, numbers); }