Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDEA] Regex: Implement named captures #59

Closed
wants to merge 11 commits into from

Conversation

ma8ma
Copy link
Owner

@ma8ma ma8ma commented Sep 19, 2021

edit(2022-01-29): 上流にマージされました (JDimproved#904)


このPRはアイデア検証用でありマージしません。

正規表現の名前付きキャプチャ機能をラッパークラスに実装します。
Glib Regexのみ有効でそれ以外の正規表現ライブラリでは名前を指定してもマッチしません。

合わせて スレタイ検索時にアドレスとスレタイを取得する正規表現 (about:config) に名前付きキャプチャ対応を追加します。
また、互換性のためグループ番号は現状を維持します。

スレタイ検索の設定

スレタイ検索時にアドレスとスレタイを取得する正規表現 (about:config) はグループ番号のかわりに名前付きキャプチャを利用することができる。( v0.6.0+から追加、Glib Regexのみ )

正規表現パターン中に名前が有れば名前付きキャプチャ、無ければグループ番号で対象を取得する。

取得対象 グループ番号 キャプチャの名前 備考
スレURL 1 url 必須
スレタイトル 2 subject 必須
レス数 3 number オプション

グループ番号は順序固定のため取得対象が番号順に並んでいないときは名前付きキャプチャを使うと分かりやすい。
(下の例はスレタイ、レス数、URLの順に並んでいるデータにマッチする)

設定例 正規表現パターン
グループ番号 ^(?=[^\t\n]+\t[^\t\n]+\t([^\t\n]+))([^\t\n]+)\t([^\t\n]+)
名前付きキャプチャ ^(?<subject>[^\t\n]+)\t(?<number>[^\t\n]+)\t(?<url>[^\t\n]+)

正規表現の構文はGlib公式など外部サイトを参照。


パッチの適応方法

git clone コマンドを使う方法 (一時的なcloneで使い捨てる)

ブランチを削除したため利用できません

git clone -b idea-regex-implement-named-capture --depth 1 https://github.com/ma8ma/JDim.git temp
cd temp

meson debugdir
meson compile -C debugdir

# 実行
./debugdir/src/jdim

# パッチの削除
cd ../
rm -rf temp

curl と patch コマンドを使う場合

※ git pullなどでmasterブランチを更新してから行うことを推奨します。

git chechout master
git pull
curl -L https://github.com/ma8ma/JDim/pull/59.patch | patch -p1
meson debugdir
meson compile -C debugdir

# 実行
./debugdir/src/jdim

# パッチの削除
rm -rf debugdir
git reset --hard master

hub コマンドを使う場合

hub checkout https://github.com/ma8ma/JDim/pull/59
meson debugdir
meson compile -C debugdir

# 実行
./debugdir/src/jdim

# パッチの削除
rm -rf debugdir
git checkout master
git branch -D idea-regex-implement-named-capture

@ma8ma ma8ma added the archive label Sep 19, 2021
@ma8ma
Copy link
Owner Author

ma8ma commented Sep 19, 2021

Oniguruma利用時にregexのテストコードのビルドでposix regexと宣言が混ざりエラーが起きるようです

https://github.com/ma8ma/JDim/pull/59/checks?check_run_id=3645171604

In file included from /usr/include/gtest/internal/gtest-port.h:380,
                 from /usr/include/gtest/internal/gtest-internal.h:40,
                 from /usr/include/gtest/gtest.h:62,
                 from ../test/gtest_jdlib_jdregex.cpp:5:
/usr/include/regex.h:478:34: error: conflicting declaration ‘typedef struct re_pattern_buffer regex_t’
  478 | typedef struct re_pattern_buffer regex_t;
      |                                  ^~~~~~~
In file included from ../src/jdlib/jdregex.h:15,
                 from ../test/gtest_jdlib_jdregex.cpp:3:
/usr/include/onigposix.h:87:3: note: previous declaration as ‘typedef struct regex_t regex_t’
   87 | } regex_t;
      |   ^~~~~~~
In file included from /usr/include/gtest/internal/gtest-port.h:380,
                 from /usr/include/gtest/internal/gtest-internal.h:40,
                 from /usr/include/gtest/gtest.h:62,
                 from ../test/gtest_jdlib_jdregex.cpp:5:
/usr/include/regex.h:521:3: error: conflicting declaration ‘typedef struct regmatch_t regmatch_t’
  521 | } regmatch_t;
      |   ^~~~~~~~~~
In file included from ../src/jdlib/jdregex.h:15,
                 from ../test/gtest_jdlib_jdregex.cpp:3:
/usr/include/onigposix.h:80:3: note: previous declaration as ‘typedef struct regmatch_t regmatch_t’
   80 | } regmatch_t;
      |   ^~~~~~~~~~
In file included from /usr/include/gtest/internal/gtest-port.h:380,
                 from /usr/include/gtest/internal/gtest-internal.h:40,
                 from /usr/include/gtest/gtest.h:62,
                 from ../test/gtest_jdlib_jdregex.cpp:5:
/usr/include/regex.h:643:12: error: conflicting declaration of C function ‘int regexec(const regex_t*, const char*, size_t, regmatch_t*, int)’
  643 | extern int regexec (const regex_t *_Restrict_ __preg,
      |            ^~~~~~~
In file included from ../src/jdlib/jdregex.h:15,
                 from ../test/gtest_jdlib_jdregex.cpp:3:
/usr/include/onigposix.h:162:20: note: previous declaration ‘int regexec(regex_t*, const char*, size_t, regmatch_t*, int)’
  162 | ONIG_EXTERN int    regexec P_((regex_t* reg, const char* str, size_t nmatch, regmatch_t* matches, int options));
      |                    ^~~~~~~

@ma8ma ma8ma force-pushed the idea-regex-implement-named-capture branch from 6a19581 to 7067724 Compare September 25, 2021 07:53
@ma8ma
Copy link
Owner Author

ma8ma commented Sep 25, 2021

clang-5.0 のコンパイルエラーのまとめ

  • 現状の実装(noexcept)ではclang-5.0以外のjobはビルドに成功する
  • noexcept(bool式)を使うとclangのjobで成功失敗が分かれる
  • noexceptの使用をやめるとすべてのjobでビルドに成功する

正規表現の名前付きキャプチャ機能をラッパークラスに実装します。
Glib Regexのみ有効でそれ以外の正規表現ライブラリでは名前を指定しても
マッチしません。

合わせて `スレタイ検索時にアドレスとスレタイを取得する正規表現`
(about:config) に名前付きキャプチャ対応を追加します。
また、互換性のためグループ番号は現状を維持します。

取得対象     | グループ番号 | キャプチャの名前 | 備考
---          | ---          | ---              | ---
スレURL      | 1            | url              | 必須
スレタイトル | 2            | subject          | 必須
レス数       | 3            | number           | オプション
oniguruma と gtest の regex プロトタイプ宣言が衝突しエラーになるため
APIのマクロをundefする
oniguruma と gtest の regex プロトタイプ宣言が衝突しコンパイルエラーに
なるためテストはスキップします。
テストプログラムのビルドの条件コンパイルを修正し正しい条件で
ビルドされるようにします。

https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Configuration-Headers.html
```
In file included from gtest_jdlib_jdregex.cpp:3:0:
../src/jdlib/jdregex.h:11:10: fatal error: config.h: No such file or directory
 #include "config.h"
          ^~~~~~~~~~
```
clang 5.0はCWG DR1778[1]を実装していないためnoexcept指定のコンストラクタで
コンパイルエラーが発生します。そのためコンパイラのバージョンをチェック
してnoexceptを指定するように変更します。

[1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1286r2.html

```
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/vector:67:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:340:20: error: exception specification of '_Vector_impl' uses itself
      _Vector_impl _M_impl;
                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:340:20: note: in instantiation of exception specification for '_Vector_impl' requested here
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/type_traits:1018:14: note: in instantiation of template class 'std::__is_nt_constructible_impl<true, std::allocator<std::__cxx11::basic_string<char> >>' requested here
    : public __is_nothrow_constructible_impl<_Tp>::type
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:132:6: note: in instantiation of template class 'std::is_nothrow_default_constructible<std::allocator<std::__cxx11::basic_string<char> > >' requested here
            is_nothrow_default_constructible<_Tp_alloc_type>::value)
            ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:340:20: note: in instantiation of exception specification for '_Vector_impl' requested here
      _Vector_impl _M_impl;
                   ^
In file included from ../src/article/articleadmin.cpp:16:
../src/article/../jdlib/jdregex.h:83:9: error: exception specification of explicitly defaulted default constructor does not match the calculated one
        Regex() noexcept = default;
        ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/type_traits:1018:14: note: in instantiation of template class 'std::__is_nt_constructible_impl<true, std::allocator<std::__cxx11::basic_string<char> >>' requested here
    : public __is_nothrow_constructible_impl<_Tp>::type
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:132:6: note: in instantiation of template class 'std::is_nothrow_default_constructible<std::allocator<std::__cxx11::basic_string<char> > >' requested here
            is_nothrow_default_constructible<_Tp_alloc_type>::value)
            ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:340:20: note: in instantiation of exception specification for '_Vector_impl' requested here
      _Vector_impl _M_impl;
                   ^
```
clangで発生するコンパイルエラーの条件が不明のためnoexceptを外して試します。
noexcept指定をもとに戻しCIでテストするコンパイラを変更します。
@ma8ma
Copy link
Owner Author

ma8ma commented Oct 2, 2021

テストの結果、現バージョン0.6.0でのマージを断念し動作環境が更新される0.7.0リリース以降に上流へマージしたいと思います。

@ma8ma ma8ma closed this Oct 2, 2021
@ma8ma ma8ma deleted the idea-regex-implement-named-capture branch October 2, 2021 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant