From 6e1cde7c8059c60ffed893fc656f0ed92566cc1d Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 5 Dec 2018 13:27:30 +0100 Subject: fix(osurl): meta version of generating lpeg parser via lpeg. Simple configuration. --- osurl | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) mode change 100755 => 100644 osurl diff --git a/osurl b/osurl old mode 100755 new mode 100644 index 2deb753..303d45f --- a/osurl +++ b/osurl @@ -1,19 +1,41 @@ -#!/usr/bin/luajit - local lpeg = require 'lpeg' -local P,C,Cs,R = lpeg.P, lpeg.C, lpeg.Cs, lpeg.R +local P,C,Cs,Cf,R = lpeg.P, lpeg.C, lpeg.Cs, lpeg.Cf, lpeg.R + +local tokens = { + NONHASH = C((1 - P("#"))^1), -- any nonempty string not containing # + NUM = C(R("09")^1), -- nonempty digit string + WORD = C(R("AZ","az","09","__")^1) -- nonempty word +} +local patterns = { + ["bgo#%NUM"] = "http://bugzilla.gnome.org/show_bug.cgi?id=%1", + ["bko#%NUM"] = "http://bugzilla.kernel.org/show_bug.cgi?id=%1", + ["bmo#%NUM"] = "http://bugzilla.mozilla.org/show_bug.cgi?id=%1", + ["boo#%NUM"] = "http://bugzilla.opensuse.org/show_bug.cgi?id=%1", + ["bsc#%NUM"] = "http://bugzilla.suse.com/show_bug.cgi?id=%1", + ["gh#%NONHASH#%WORD"] = "https://github.com/%1/issues/%2", + ["lp#%NUM"] = "https://launchpad.net/bugs/%1", + ["rh#%NUM"] = "http://bugzilla.redhat.com/show_bug.cgi?id=%1" +} + +-- turn "foo#%BLAH" into a pattern that matches everything literally except +-- %BLAH +local makepat = Cf( + ( + ((P("%") * C(R("AZ")^1)) / tokens) -- %BLAH is from tokens + + (P"%%" / function() return P"%" end) -- %% treat as literal % + + (C(P(1)) / P) -- anything else is literal + )^0, + function(a,b) return a * b end) +local function make_pattern(str) + return assert(makepat:match(str), "invalid pattern") +end -local cnum = C(R("09")^1) -local cword = C(R("AZ","az","09","__")^1) -local bugpat = - (P"bgo#" * cnum) / "http://bugzilla.gnome.org/show_bug.cgi?id=%1" - + (P"bko#" * cnum) / "http://bugzilla.kernel.org/show_bug.cgi?id=%1" - + (P"bmo#" * cnum) / "http://bugzilla.mozilla.org/show_bug.cgi?id=%1" - + (P"boo#" * cnum) / "http://bugzilla.opensuse.org/show_bug.cgi?id=%1" - + (P"bsc#" * cnum) / "http://bugzilla.suse.com/show_bug.cgi?id=%1" - + (P"gh#" * C((1 - P"#")^1) * P"#" * cword) / "https://github.com/%1/issues/%2" - + (P"lp#" * cnum) / "https://launchpad.net/bugs/%1" - + (P"rh#" * cnum) / "http://bugzilla.redhat.com/show_bug.cgi?id=%1" +local bugpat = P(false) +for k,v in pairs(patterns) do + bugpat = bugpat + (make_pattern(k) / v) +end local pat = Cs((bugpat + P(1))^0) -os.execute("xdg-open " .. pat:match(arg[1])) +print(pat:match("gh#python/cpython#123")) +print(pat:match("lp#123456")) +print(pat:match("bramboříček")) -- cgit