Comments (13)
Here is a quick and rather untested ad hoc definition of term//1
:
:- use_module(library(dcgs)). :- use_module(library(charsio)). :- use_module(library(lists)). term(T) --> call(term_(T)). term_(T, Cs0, Cs) :- append(Prefix, Cs, Cs0), catch(read_from_chars(Prefix, T), error(syntax_error(_),_), false).
Example:
?- phrase(("<",term(T),">"), "< hello. >"). T = hello ; false.
Tested with Scryer Prolog.
from trealla.
A DCG nonterminal would be perfect. I'm currently using a DCG to look for delimiters (<?
and ?>
in my case 😉) and then calling read_from_chars/3
, but I could use term//1
directly to do that quite elegantly.
Thanks for the workaround as well @triska!
from trealla.
In Trealla that is...
term(T) -->
call(term_(T)).
term_(T, Cs0, Cs) :-
append(Prefix, Cs, Cs0),
catch(read_term_from_chars(T, Prefix), error(syntax_error(_),_), false).
?- phrase(("<",term(T),">"), "< hello. >").
T = hello
; false.
EDIT: note this is no longer correct. it is.. catch(read_term_from_chars(Prefix,T), error(syntax_error(),), false).
EDIT2: or use the code directly as provided by Markus.
from trealla.
I think a DCG nonterminal term//1
would be useful for this. With it, one could use phrase/3
to read a term, and have the remainder available for further reasoning.
For example, one could read two terms with:
?- phrase((term(T1),term(T2),...), Cs).
or, using list differences, with:
?- phrase(term(T1), Cs0, Cs1), phrase(term(T2), Cs1, Cs2).
where Cs2
is the remainder after T1
and T2
are read from the list of characters Cs0
.
from trealla.
By the way, as a workaround, until such a nonterminal is available, you can manually try every possible string prefix to read a term:
?- append(Prefix, Rest, Cs), read_from_chars(Prefix, T).
with suitable use of catch/3
to catch syntax errors, failing if Prefix
cannot (yet) be parsed. This incurs quadratic overhead, which is unacceptable for longer strings, but may be acceptable for the present use case, and until a faster (linear) construct is available.
from trealla.
$ tpl
?- help(read_term_from_chars/2).
read_term_from_chars/2: read_term_from_chars(+chars,?term)
true.
?- help(read_term_from_chars/3).
read_term_from_chars/3: read_term_from_chars(+chars,?term,+list)
true.
from trealla.
Personally, I think a good building block would be read_from_chars_(T, Cs0, Cs)
, reading from Cs0
the term T
, leaving remainder Cs
. This can be used (internally, since it relies on the implementation dependent order of arguments) in DCGs, and we can also (portably) implement term//1
and read_term_from_chars/3
on top of it.
For example, with this building block, term//1
could be implemented as:
term(T) --> call(read_from_chars_(T)).
from trealla.
Sounds good.
from trealla.
from trealla.
Is this supposed to happen?
?- '$read_term_from_chars'(T,[],"abc.",Rest).
T = abc, Rest = "abc.".
Puts my naïve DCG into an infinite loop.
from trealla.
from trealla.
This is working quite nicely now. Very quick and no problem with decimal points. Thanks a bunch!
Here it is in action: https://php.energy/moon.html
from trealla.
from trealla.
Related Issues (20)
- Meta predicate goal expansion HOT 2
- Segmentation fault (core dumped)
- FYI: Trealla trashes effort to port a C-compiler HOT 11
- Pressing the tab key = segmentation fault HOT 3
- CLPZ: `#<==>/2` crashing (and consequently: `clpz_t/2`, `#=/3`, `#</3`) HOT 6
- CLPZ: Comparison HOT 3
- current_op/3 shuttered
- `phrase_to_stream/2` (consequently: `phrase_to_file/2`) total memory consumption with a mega-string HOT 1
- is/2 type_error disregarded HOT 1
- CLPZ:monotonic: segfault
- CLPZ: Error: syntax error unexpected HOT 2
- segmentation fault in warehouse test HOT 5
- phrase/2 instantiation_error? HOT 1
- phrase/2 segfault HOT 1
- does this terminate, i.e. model finder HOT 8
- Application-specific Comparsion HOT 4
- Odd parser bug HOT 8
- Syntax error undetected
- op/3 Operator deactivation fault
- garbage collection and keysort/2 HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trealla.