Skip to content

Commit

Permalink
without passing support
Browse files Browse the repository at this point in the history
  • Loading branch information
sirkon committed Mar 17, 2019
1 parent 9a5233a commit b7cd26b
Show file tree
Hide file tree
Showing 75 changed files with 1,492 additions and 13,454 deletions.
14 changes: 14 additions & 0 deletions LDE.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ baseAction

atomicAction
: passTargetPrefix
| checkTargetPrefix
| passHeadingCharacters
| mayBePassTargetPrefix
| passChars
| passUntil
| mayPassUntil
| goUntil
| mayGoUntil
| takeUntil
| takeUntilIncluding
| takeUntilOrRest
Expand All @@ -42,6 +45,11 @@ passTargetPrefix
| '^' targetLit
;

checkTargetPrefix
: '@' targetLit '[' IntLit ']'
| '@' targetLit
;

mayBePassTargetPrefix
: '?' '^' targetLit '[' IntLit ']'
| '?' '^' targetLit
Expand All @@ -50,6 +58,12 @@ mayBePassTargetPrefix
passChars
: '_' '[' IntLit ':' ']';

goUntil
: '..' target;

mayGoUntil
: '?' '..' target;

passUntil
: '_' target;

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test:
PATH=${GOPATH}/bin:${PATH}
go install github.com/sirkon/ldetool
go install
go get -u github.com/stretchr/testify
go get -u github.com/sirkon/decconv
go generate github.com/sirkon/ldetool/testing
Expand Down
7 changes: 6 additions & 1 deletion TOOL_RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ Rule and capture names must be public and starts from capital letter (i.e. `Name
|``^'c'``|Check if the rest starts with the given character *c* and pass it.<br>Signal error otherwise|``^'@'("@usr") → "usr"``|
|``?^`c'``|If the rest starts with the given character *c* then pass it|``?^'@'("@usr") → "usr"``<br>``?^'@'("usr") → "usr"``|
|``^"t"``|Check if the rest starts with the given text *t* and pass it.<br> Signal error otherwise|``^"ab"("ab12") → "12"``|
|``@"t"``|Check if the rest starts with the given text *t* without passing it.<br> Signal error otherwise|``@"ab"("ab12") → "ab12"``|
|``?^"t"``|If the rest starts with the given text *t* then pass it|``^"ab"("ab12") → "12"``<br>``^"ab"("a12") → "a12"``|
|``_'c'``|Look for the character *c* in the rest and pass it.<br>Signal error when it was not found|``_'1'("a12") → "2"``|
|``..'c'``|Look for the character *c* in the rest without passing it.<br>Signal error when it was not found|``..'1'("a12") → "12"``|
|``_?'c'``|Works exactly like ``_'c'`` if the character *c* was found.<br>Do nothing otherwise|``_'1'("a12") → "2"``<br>``_'1'("a2") → "a2"``|
|``_'c'[:N]``|Look for the character *c* in first N characters the rest and pass it.<br>Signal error when it was not found|``_'1'[:2]("a12") → "2"``<br>``_'1'[:2]("aa12") → error``<br>``_'1'[:3]("aa123c") → "23c"``|
|``..?'c'``|Works exactly like ``..'c'`` if the character *c* was found.<br>Do nothing otherwise|``..'1'("a12") → "12"``<br>``..'1'("a2") → "a2"``|
|``'c'[:N]``|Look for the character *c* in first N characters the rest and pass it.<br>Signal error when it was not found|``_'1'[:2]("a12") → "2"``<br>``_'1'[:2]("aa12") → error``<br>``_'1'[:3]("aa123c") → "23c"``|
|``_?'c'[:N]``|Look for the character *c* in first N characters the rest and pass it.<br>Ignore when text *t* was not found|``_'1'[:2]("a12") → "2"``<br>``_'1'[:2]("aa12") → "aa12"``<br>``_'1'[:3]("aa123c") → "23c"``|
|``_'c'[M:N]``|Look for the character *c* in the M..N-1 characters of the rest<br>and pass it.<br>Signal error when it was not found|``_'1'[1:2]("a12") → "2"``<br>``_'1'[1:2]("12") → error``<br>``_'1'[0:2]("123c") → "23c"``|
|``_'c'[M:]``|Look for the character *c* in the M, M+1, etc characters of the rest<br>and pass it.<br>Signal error when it was not found|``_'1'[1:]("a12") → "2"``<br>``_'1'[1:]("12") → error``<br>``_'1'[0:]("123c") → "23c"``|
Expand All @@ -82,6 +85,8 @@ Rule and capture names must be public and starts from capital letter (i.e. `Name
|``_"t"[M]``|Symbols of the rest from (M+1)-th position must starts with *t* |``_"ab"[1:3]("1ab2") → "2"``<br>``_?"ab"[2:4]("1ab2") → error``|
|``_?"t"[M:N]``| |``_?"ab"[1:3]("1ab2") → "2"``<br>``_?"ab"[2:4]("1ab2") → "1ab2"``|
> Notice, each `_<something>` action except `_[N:]` has its `..<something>` counterpart which works exactly like `_` except it stops right before the target without passing it.
#### Note
You can put `~` sign before a char or string you are looking for. This means "short" lookup: for loop will be used for
char lookup instead of `bytes.IndexByte`:
Expand Down
3 changes: 3 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate antlr4 -visitor -no-visitor -listener -o internal/parser -Dlanguage=Go LDE.g4
14 changes: 4 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
module github.com/sirkon/ldetool/v3
module github.com/sirkon/ldetool

require (
github.com/antlr/antlr4 v0.0.0-20190207013812-1c6c62afc7cb
github.com/antlr/antlr4 v0.0.0-20190313170020-28fc84874d7f
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/kr/pretty v0.1.0 // indirect
github.com/sanity-io/litter v1.1.0
github.com/sirkon/decconv v1.0.0
github.com/sirkon/gosrcfmt v1.5.0
github.com/sirkon/gotify v0.5.0
github.com/sirkon/ldetool/internal v0.0.0-00010101000000-000000000000
github.com/sirkon/gotify v0.6.0
github.com/sirkon/message v1.5.1
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
)

replace github.com/sirkon/ldetool/internal => ./internal
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"fmt"
)

var _ Action = &PassUntil{}
var _ Action = &PassAfter{}

// PassUntil ...
type PassUntil struct {
// PassAfter ...
type PassAfter struct {
access
Limit *Target
}

func (pu *PassUntil) Accept(d ActionDispatcher) error {
return d.DispatchPassUntil(pu)
func (pu *PassAfter) Accept(d ActionDispatcher) error {
return d.DispatchPassAfter(pu)
}

func (pu *PassUntil) String() string {
func (pu *PassAfter) String() string {
if pu.Limit.Lower == pu.Limit.Upper && pu.Limit.Lower > 0 {
switch pu.Limit.Type {
case String:
Expand All @@ -38,9 +38,9 @@ func (pu *PassUntil) String() string {
return res
}

// PassUntilTarget ...
func PassUntilTarget() *PassUntil {
return &PassUntil{
// PassAfterTarget ...
func PassAfterTarget() *PassAfter {
return &PassAfter{
Limit: NewTarget(),
}
}
31 changes: 31 additions & 0 deletions internal/ast/action_pass_after_or_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ast

var _ Action = &PassAfterOrIgnore{}

// PassAfterOrIgnore ...
type PassAfterOrIgnore struct {
access
Limit *Target
}

func (p *PassAfterOrIgnore) Accept(d ActionDispatcher) error {
return d.DispatchPassAfterOrIgnore(p)
}

func (p *PassAfterOrIgnore) String() string {
pu := &PassAfter{
Limit: p.Limit,
}
if p.Limit.Lower == p.Limit.Upper && p.Limit.Lower > 0 {
return pu.String() + " or ignore otherwise"
} else {
return pu.String() + " or ignore if not found"
}
}

// PassAfterTargetOrIgnore ...
func PassAfterTargetOrIgnore() *PassAfterOrIgnore {
return &PassAfterOrIgnore{
Limit: NewTarget(),
}
}
46 changes: 46 additions & 0 deletions internal/ast/action_pass_before.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ast

import (
"fmt"
)

var _ Action = &PassBefore{}

// PassBefore ...
type PassBefore struct {
access
Limit *Target
}

func (pu *PassBefore) Accept(d ActionDispatcher) error {
return d.DispatchPassBefore(pu)
}

func (pu *PassBefore) String() string {
if pu.Limit.Lower == pu.Limit.Upper && pu.Limit.Lower > 0 {
switch pu.Limit.Type {
case String:
return fmt.Sprintf("Check if the rest at %s character and further starts with prefix %s and pass until it", posLit(pu.Limit.Lower+1), pu.Limit.Value)
case Char:
return fmt.Sprintf("Check if %s character equals to %s and pass until it", posLit(pu.Limit.Lower+1), pu.Limit.Value)
}
}

var area string
if pu.Limit.Lower > 0 && pu.Limit.Upper > 0 {
area = fmt.Sprintf("rest[%d:%d]", pu.Limit.Lower, pu.Limit.Upper)
} else if pu.Limit.Lower > 0 {
area = fmt.Sprintf("rest[%d:]", pu.Limit.Lower)
} else {
area = "the rest"
}
res := fmt.Sprintf("Look for \033[1m%s\033[0m in %s without passing it", pu.Limit.Value, area)
return res
}

// PassBeforeTarget ...
func PassBeforeTarget() *PassBefore {
return &PassBefore{
Limit: NewTarget(),
}
}
31 changes: 31 additions & 0 deletions internal/ast/action_pass_before_or_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ast

var _ Action = &PassBeforeOrIgnore{}

// PassBeforeOrIgnore ...
type PassBeforeOrIgnore struct {
access
Limit *Target
}

func (p *PassBeforeOrIgnore) Accept(d ActionDispatcher) error {
return d.DispatchPassBeforeOrIgnore(p)
}

func (p *PassBeforeOrIgnore) String() string {
pu := &PassBefore{
Limit: p.Limit,
}
if p.Limit.Lower == p.Limit.Upper && p.Limit.Lower > 0 {
return pu.String() + " or ignore otherwise"
} else {
return pu.String() + " or ignore if not found"
}
}

// PassBeforeTargetOrIgnore ...
func PassBeforeTargetOrIgnore() *PassBeforeOrIgnore {
return &PassBeforeOrIgnore{
Limit: NewTarget(),
}
}
31 changes: 0 additions & 31 deletions internal/ast/action_pass_until_or_ignore.go

This file was deleted.

27 changes: 27 additions & 0 deletions internal/ast/action_start_char_without_pass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ast

import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
)

var _ Action = &StartCharWithoutPass{}

// StartCharWithoutPass ...
type StartCharWithoutPass struct {
access
Value string
}

func (sc *StartCharWithoutPass) Accept(d ActionDispatcher) error {
return d.DispatchStartCharWithoutPass(sc)
}

func (sc *StartCharWithoutPass) String() string {
return fmt.Sprintf("Check and pass character \033[1m%s\033[0m", sc.Value)
}

// StartsWithCharWithoutPass ...
func StartsWithCharWithoutPass(target antlr.Token) *StartCharWithoutPass {
return &StartCharWithoutPass{Value: target.GetText()}
}
28 changes: 28 additions & 0 deletions internal/ast/action_start_string_without_pass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ast

import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
)

var _ Action = &StartStringWithoutPass{}

// StartStringWithoutPass ...
type StartStringWithoutPass struct {
access
Value string
}

func (ss *StartStringWithoutPass) Accept(d ActionDispatcher) error {
return d.DispatchStartStringWithoutPass(ss)
}

func (ss *StartStringWithoutPass) String() string {
return fmt.Sprintf("Check and pass \033[1m%s\033[0m", ss.Value)

}

// StartsWithStringWithoutPass constructor
func StartsWithStringWithoutPass(target antlr.Token) *StartStringWithoutPass {
return &StartStringWithoutPass{Value: target.GetText()}
}
8 changes: 6 additions & 2 deletions internal/ast/action_z_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ type ActionDispatcher interface {
DispatchOptional(a *Optional) error
DispatchPassHeadingCharacters(a PassHeadingCharacters) error
DispatchPassFirst(a PassFixed) error
DispatchPassUntil(a *PassUntil) error
DispatchPassUntilOrIgnore(a *PassUntilOrIgnore) error
DispatchPassAfter(a *PassAfter) error
DispatchPassAfterOrIgnore(a *PassAfterOrIgnore) error
DispatchPassBefore(a *PassBefore) error
DispatchPassBeforeOrIgnore(a *PassBeforeOrIgnore) error
DispatchStartChar(a *StartChar) error
DispatchStartCharWithoutPass(a *StartCharWithoutPass) error
DispatchStartString(a *StartString) error
DispatchStartStringWithoutPass(a *StartStringWithoutPass) error
DispatchTake(a *Take) error
DispatchTakeIncluding(a *TakeIncluding) error
DispatchTakeRest(a *TakeRest) error
Expand Down
12 changes: 6 additions & 6 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type Generator interface {
AtEnd() error

// Head
HeadString(anchor string, ignore bool) error
HeadChar(char string, ignore bool) error
HeadString(anchor string, ignore bool, pass bool) error
HeadChar(char string, ignore bool, pass bool) error

// Lookups
LookupString(anchor string, lower, upper int, close, ignore bool) error
LookupFixedString(anchor string, offset int, ignore bool) error
LookupChar(anchor string, lower, upper int, close, ignore bool) error
LookupFixedChar(anchor string, offset int, ignore bool) error
LookupString(anchor string, lower, upper int, close, ignore, pass bool) error
LookupFixedString(anchor string, offset int, ignore, pass bool) error
LookupChar(anchor string, lower, upper int, close, ignore, pass bool) error
LookupFixedChar(anchor string, offset int, ignore, pass bool) error

// Takes
// Take before anchor (string or character)
Expand Down
Loading

0 comments on commit b7cd26b

Please sign in to comment.