Verified Commit 1e1f92b2 authored by Camil Staps's avatar Camil Staps 🙂

Use regular expressions for syntax pattern matching

parent 23b05936
......@@ -7,3 +7,6 @@
[submodule "libcloogle"]
path = libcloogle
url = https://github.com/clean-cloogle/libcloogle.git
[submodule "CleanRegex"]
path = CleanRegex
url = https://github.com/camilstaps/CleanRegex
Subproject commit 2c5edcee074fff52cd6e5ab188b506a40f362a6c
......@@ -11,6 +11,8 @@ from Database.Native import :: NativeDB, :: Entry, :: Index
from System.FilePath import :: FilePath
from Text.GenJSON import generic JSONEncode, generic JSONDecode, :: JSONNode
from Regex import :: Regex
from Clean.Types import :: Type, :: TypeVar, :: TVAssignment, :: TypeDef,
:: TypeContext, :: TypeRestriction, :: Priority
from Clean.Types.Tree import :: TypeTree
......@@ -181,7 +183,7 @@ location :: !Library !String !FilePath !LineNr !LineNr !Name -> Location
*/
:: SyntaxEntry =
{ syntax_title :: String //* The name of the construct
, syntax_patterns :: [String] //* Patterns to search for the construct
, syntax_patterns :: [Regex] //* Patterns to search for the construct
, syntax_code :: [String] //* Strings describing the construct, as short as possible
, syntax_description :: String //* A description for documentation
, syntax_doc_locations :: [CleanLangReportLocation] //* Where to find documentation on the construct
......@@ -199,13 +201,6 @@ location :: !Library !String !FilePath !LineNr !LineNr !Name -> Location
, aie_description :: String //* A description for documentation
}
/**
* A search pattern for syntax constructs
*/
:: SyntaxPattern :== String
patternMatches :: !SyntaxPattern !String -> Bool
:: Name :== String
:: Library :== String
:: Module :== String
......
......@@ -38,6 +38,8 @@ import System.FilePath
from Text import class Text(concat), instance Text String
import Text.GenJSON
import Regex
import Clean.Types
import Clean.Types.Tree
import Clean.Types.Util
......@@ -64,13 +66,13 @@ derive JSONEncode ClassDoc, ClassEntry, ClassMemberDoc, CloogleEntry,
InstanceEntry, Location, ModuleDoc, ModuleEntry, Priority, RecordField,
SyntaxEntry, Type, TypeDef, TypeDefEntry, TypeDefRhs, TypeDoc,
TypeRestriction, ABCInstructionEntry, Property, PropertyVarInstantiation,
MultiLineString, NGramIndex, PropertyTestGenerator
MultiLineString, NGramIndex, PropertyTestGenerator, Regex, GroupId
derive JSONDecode ClassDoc, ClassEntry, ClassMemberDoc, CloogleEntry,
Constructor, ConstructorDoc, DeriveEntry, FunctionDoc, FunctionEntry,
InstanceEntry, Location, ModuleDoc, ModuleEntry, Priority, RecordField,
SyntaxEntry, Type, TypeDef, TypeDefEntry, TypeDefRhs, TypeDoc,
TypeRestriction, ABCInstructionEntry, Property, PropertyVarInstantiation,
MultiLineString, NGramIndex, PropertyTestGenerator
MultiLineString, NGramIndex, PropertyTestGenerator, Regex, GroupId
printersperse :: Bool a [b] -> [String] | print a & print b
printersperse ia a bs = intercalate (print False a) (map (print ia) bs)
......@@ -131,25 +133,6 @@ where
| fe.fe_kind == RecordField = "." +++ f
| otherwise = f
patternMatches :: !SyntaxPattern !String -> Bool
patternMatches p s = m [c \\ c <-: p] [c \\ c <-: s]
where
// %: any character
// *: any number of characters (inefficient!)
// \*: the character '*'
// \w: a word character (a-zA-Z_`)
// \e: a word or digit character (a-zA-Z0-9_`)
// other characters are matched literally
m :: [Char] [Char] -> Bool
m [] [] = True
m ['%':p] [_:s] = m p s
m ['*':p] s = any (m p) (tails s)
m ['\\':'*':p] [x:s] = '*' == x && m p s
m ['\\':'w':p] [x:s] = (isAlpha x || x == '_' || x == '`') && m p s
m ['\\':'e':p] [x:s] = (isAlpha x || isDigit x || x == '_' || x == '`') && m p s
m [c:p] [x:s] = c == x && m p s
m _ _ = False
location :: !Library !String !FilePath !LineNr !LineNr !Name -> Location
location lib mod fp dcl icl name = Location lib mod fp dcl icl name
......@@ -415,8 +398,10 @@ filterName s wrap=:{db,name_ngrams,syntax,abc_instrs}
where
indices = [(i,[(MatchingNGrams,n)]) \\ (i,n) <- 'NGrams'.search s name_ngrams]
cs = [c \\ c <-: s]
syntaxSearch :: CloogleEntry -> (Bool, [a])
syntaxSearch (SyntaxEntry se) = (any (flip patternMatches s) se.syntax_patterns, [])
syntaxSearch (SyntaxEntry se) = (any (not o isEmpty o flip match cs) se.syntax_patterns, [])
syntaxSearch _ = (False, [])
abcSearch :: CloogleEntry -> (Bool, [a])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment