-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from nevalang/regexp_submatcher
feat(std:regexp): Submatcher (+ example)
- Loading branch information
Showing
7 changed files
with
74 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { regexp } | ||
|
||
const { regex string = 'a(x*)b(y|z)c' } | ||
|
||
component Main(start) (stop) { | ||
nodes { | ||
Printer<any> | ||
|
||
#bind(regex) | ||
regexp.Submatcher | ||
} | ||
net { | ||
:start -> ('-axxxbyc-' -> submatcher:data) | ||
submatcher:res -> printer:data | ||
printer:sig -> :stop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package funcs | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
|
||
"github.com/nevalang/neva/internal/runtime" | ||
) | ||
|
||
type regexpSubmatcher struct{} | ||
|
||
func (r regexpSubmatcher) Create(io runtime.FuncIO, cfgMsg runtime.Msg) (func(ctx context.Context), error) { | ||
regex, err := regexp.Compile(cfgMsg.Str()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dataIn, err := io.In.Port("data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resOut, err := io.Out.Port("res") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case dataMsg := <-dataIn: | ||
resOut <- wrap( | ||
regex.FindStringSubmatch( | ||
dataMsg.String(), | ||
), | ||
) | ||
} | ||
} | ||
}, nil | ||
} | ||
|
||
func wrap(ss []string) runtime.Msg { | ||
msgs := make([]runtime.Msg, 0, len(ss)) | ||
for _, s := range ss { | ||
msgs = append(msgs, runtime.NewStrMsg(s)) | ||
} | ||
return runtime.NewListMsg(msgs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pkg | ||
|
||
// Version is the current version of the language and stdlib | ||
var Version = "0.9.0" //nolint:gochecknoglobals | ||
var Version = "0.10.0" //nolint:gochecknoglobals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// bind regexp string | ||
#extern(regexp_submatcher) | ||
pub component Submatcher(data string) (res list<string>) |
This file was deleted.
Oops, something went wrong.