ethanblake4 / dart_eval

Extensible Dart interpreter for Dart with full interop
https://pub.dev/packages/dart_eval
BSD 3-Clause "New" or "Revised" License
334 stars 40 forks source link

Support For Regex #132

Closed Noobware1 closed 1 year ago

Noobware1 commented 1 year ago

I mean support for like first match, last match, group and groups methods

@ethanblake4 if you at least tell me how to add it (even a rough explanation would work) i will try to follow through with the best of my abilities.

since i really love this project and would like to contribute.

ethanblake4 commented 1 year ago

For sure, thanks for your interest in helping! It's basically the same as making your own bridge wrappers, you'd just have to edit shared/stdlib/core/regexp.dart to add those methods. We don't currently have a wrapper for the RegExpMatch type, but you can just return a regular Match (using $Match) or you could add it if you want. And then just make a pull request against the v0.7.0 branch.

Noobware1 commented 1 year ago

Thanks will do!

Noobware1 commented 1 year ago

Hi, @ethanblake4 I did what you told but i am getting some issues with writing the tests

  1. with null assertion operator - if i use ! ilke this firstMatch(string)! i get this error even tho it's not null because if i use ? it returns string
    dart_eval runtime exception: Null check operator used on a null value
    #0      $Object._equals (package:dart_eval/src/eval/shared/stdlib/core/object.dart:266:41)
    #1      $Function.call (package:dart_eval/src/eval/runtime/function.dart:122:16)
    #2      CheckEq.run (package:dart_eval/src/eval/runtime/ops/objects.dart:152:14)
    at main()

    2 with return type with if try to return int or string i get an error but if i don't specify the return type it pass the test with no error

    CompileError: Cannot return RegExpMatch (expected: int) at unknown (file package:example/main.dart)
    package:dart_eval/src/eval/compiler/compiler.dart 497:7   Compiler.compileSources
    package:dart_eval/src/eval/compiler/compiler.dart 164:12  Compiler.compile
    package:dart_eval/src/eval/compiler/compiler.dart 547:21  Compiler.compileWriteAndLoad

here are the test cases

import 'package:test/test.dart';
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/stdlib/core.dart';

void main() {
  late Compiler compiler;

  setUp(() {
    compiler = Compiler();
  });
  group('Regex Tests', () {
    test('RegExp.firstMatch()', () {
      final runtime = compiler.compileWriteAndLoad({
        'example': {
          'main.dart': r'''
            String main() {
              final string = '[00:13.37] This is a chat message.';
              final regExp = RegExp(r'c\w*');
              final match = regExp.firstMatch(string)!;
              return match[0];
            }
          ''',
        }
      });
      expect(
          (runtime.executeLib('package:example/main.dart', 'main') as $String)
              .$value,
          'chat');
    });
    test('RegExp.groupCount', () {
      final runtime = compiler.compileWriteAndLoad({
        'example': {
          'main.dart': '''
            int? main() {
              final string = '[00:13.37] This is a chat message.';
              final regExp = RegExp(r'c\\w*');
              final match = regExp.firstMatch(string);
              return match?.groupCount;
            }
          ''',
        }
      });
      expect(
          (runtime.executeLib('package:example/main.dart', 'main') as $int)
              .$value,
          0);
    });
}

If need to see my code tell me how to send it thank you

ethanblake4 commented 1 year ago

Just pushed (I think) a fix for the second issue to v0.7.0 branch. First one will have to wait until tomorrow, but I'm 99% sure it's not your fault, the ! and ?. operators were just added a few days ago and they definitely still have bugs.

Noobware1 commented 1 year ago

Then I'll open a pr when all the tests pass

ethanblake4 commented 1 year ago

Released in v0.7.0