Coder Social home page Coder Social logo

jsregexp's People

Contributors

kmarius avatar l3mon4d3 avatar nathanpage-credo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jsregexp's Issues

Raise error on regex compilation fail

For both python and javascript the standard behavior is to raise an error when compilation fails. I think it would be good if the default is the same for jsregexp.compile(). A separate jsregexp.compile_safe() function can be created that prevents error raising. This is a common place practice for other such projects like cjson.

Remove `__call` metamethod for compiled regexp objects

Pinging @L3MON4D3

I would like to remove the old, undocumented __call metamethod at some point in the future because it duplicates a lot of the matching logic.

As far as I can tell, LuaSnip only uses the begin_ind property and the capture groups of a match. A simple migration step could look like this: when loading jsregexp, check if the __call metamethod exists, and if not set to something like this:

local jsregexp = require("jsregexp")
local meta = getmetatable(jsregexp.compile(""))

meta.__call = function(self, str)
	local match = self:exec(str)
	if not match then
		return {}
	end
	return {
		{
			begin_ind = match.index,
			groups = { unpack(match) },
		},
	}
end

This should work as long as only the first match is needed and the global flag is not set. A full replacement looks more like this (Note that the match object already contains the full match in match[0] which could be used directly by LuaSnip):

meta.__call = function(self, s)
	local jstr = jsregexp.to_jsstring(s)
	self.last_index = 1
	local matches = {}
	while true do
		local match = self:exec(jstr)
		if match == nil then
			break
		end
		table.insert(matches, {
			begin_ind = match.index,
			end_ind = match.index + #match[0] - 1,
			groups = { unpack(match) },
			named_groups = match.groups,
		})
                if not self.global then
                        break
                end
		if #match[0] == 0 then
			self.last_index = self.last_index + 1
		end
	end
	return matches
end

Opinions? I am not planning to release anything until I have the OK, and there is no urgency whatsoever.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.