Suppose I have this code:
(handler-case (read ...)
(parse-error (condition)
(format t "What text was I reading last to get this error? ~s~&"
(how-to-get-this-text? condition))))
I can only see the parse-namestring accessors, but it gives the message of the error, not the text it was parsing.
EDIT
In my case the problem is less generic, so an alternative solution not involving the entire string that failed to parse can be good too.
Imagine this example code I'm trying to parse:
prefix(perhaps (nested (symbolic)) expressions))suffix
In some cases I need to stop on "suffix" and in others, I need to continue, the suffix itself has no other meaning but just being an indicator of the action the parser should take next.
READ parses from a stream, not a string. The s-expression can be arbitrarily long. Should READ keep a string of what's been read?
What you might need is a special stream. In standard Common Lisp there is no mechanism for user defined streams. But in real life every implementation has such extensible streams. See for example 'gray streams'.
http://www.sbcl.org/1.0/manual/Gray-Streams.html
There's no standard function to do it. You might be able to brute-force something with read-from-string, but whatever you do, it will require some extra work.
I am getting text from a feed that has alot of characters like:
Insignia™ 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation Apple® iPod® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar
I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html
does anyone knows a file format for configuration files easy to read by humans? I want to have something like tag = value where value may be:
String
Number(int or float)
Boolean(true/false)
Array(of String values, Number values, Boolean values)
Another structure(it will be more clear what I mean in the fallowing example)
Now I use something like this:
IntTag=1
FloatTag=1.1
StringTag="a string"
BoolTag=true
ArrayTag1=[1 2 3]
ArrayTag2=[1.1 2.1 3.1]
ArrayTag3=["str1" "str2" "str3"]
StructTag=
{
NestedTag1=1
NestedTag2="str1"
}
and so on.
Parsing is easy but for large files I find it hard to read/edit in text editors. I don't like xml for the same reason, it's hard to read. INI does not support nesting and I want to be able to nest tags. I also don't want a complicated format because I will use limited kind of values as I mentioned above.
Thanks for any help.
What about YAML ? It's easy to parse, nicely structured has wide programming language support. If you don't need the full feature set, you could also use JSON.
Try YAML - is (subjectively) easy to read, allows nesting, and is relatively simple to parse.
Closed. This question is opinion-based. It is not currently accepting answers.
Closed 8 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I have a function, parseQuery, that parses a SQL query into an abstract representation of that query.
I'm about to write a function that takes an abstract representation of a query and returns a SQL query string.
What should I call the second function?
I think the verb you want is 'compose'.
The opposite of parse is serialize
In compiler terminology, the opposite is "unparse". Specifically, parsing turns a stream of tokens into abstract syntax trees, while unparsing turns abstract syntax trees into a stream of tokens.
Compose? When parsing a query you break it into its constituent parts (tokens, etc.), the reverse would be composing the parts into a string query.
To complement your existing naming, composeQuery looks best.
But in the general case, the opposite of parse is ǝsɹɐd
I would use one of these:
ToString()
ToSQL()
Render()
I think "serialize" is probably the word you want. It means to produce a textual representation of data that can be exported (and imported) from the program.
The antonym of 'analyze' is 'synthesize'.
ToQueryString()
Definitely Render.
I would call it constructQuery.
generate or emit, possibly.
Just to add some stuff.
Surely parse is a two way word.
You can parse an abstract into a query.
You can parse a query into an abstract.
The question should be, what do you name the latter part of the method, and because in this instance you're parsing an abstract to make a query you'd call it parseAbstract.
To answer the question, parsing has no opposite.
generateQuery, possibly? createQuery?
Take your pick
Generate
Dump
Serialize
Emit
They each have slightly different connotations.
Maybe prettyPrintQuery?
compose, construct, generate, render,condense, reduce, toSQL, toString depending on the nature of the class and its related operators
A traditional compiler has two parts: a parser and a code generator.
So you could call it "Generate". Of course, it's a little bit different here because the compiler isn't writing source code. (unless it's a precompiler).
Possibly Format(). or ToSQL() in your instance?
unParse()? Just kidding, I would go with toQueryString()
flatten?
The parsed query object perhaps represents a condition hierarchy, which you are "flattening" back into a 1 dimensional string.
But given that you're going from object to string, really just use toString or toSQL() or something like that. Besides, if you designed it well and are using the right app, you can rename it later and just stick stuff in the comments on what it does.
I'd say serialize and deserialize, instead of parse and ...
I would go for ToString(), since you can usually chain-nest them (opposite functions, that let you pass from Class1 to Class2 and vice-versa)
DateTime.Parse( DateTime.Parse( myDate.ToString() ).ToString() );
Serialize() looks like a nice choice, but it already has an opposite in Deserialize().
In your specific scenario, as other pointed out, ToSql() is another good choice.
I'd use render
> a = 'html': { 'head': {'title': 'My Page'}, 'body': { 'h1': 'Hello World', 'p': 'This is a Paragraph' } }
> b = render(a)
> console.log(b)
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World</h1>
<p>This is a Paragraph</p>
</body>
</html>
Which is IMHO, the opposite to parse()
> c = parse(b)
{ 'html': {
'head': {
'title': 'My Page'
}
'body': {
'h1': 'Hello World',
'p': 'This is a Paragraph'
}
}
+1 for Generate, but tack on what you're generating, i.e. GenerateSQL()
I voted for 'compose' but if you don't like that I would also suggest 'build'
What about asSQL() or even more asQuery()?
INHO Serialize, synthesize are good options. Also, as you have named parseQuery, i will go with codeQuery
I usually use "parse" as a conversion method and, therefore, i can't find a opposite word for "convert". (you can't "deconvert" something, as "unconvert" is a type of conversion itself).
thinking this way, the best solution (for me) is having two "parse" methods that receive different arguments. Example (Java):
public class FooBarParser{
public Foo parse(Bar bar);
public Bar parse(Foo foo);
}
deparse
Deparse is to parse, as:
decompile is to compile
decompose is to compose
deserialize is to serialize
degroovy is to groovy :) ;)
Parsing / deparsing is not change of structure, but conversion. Precise conversion between equivalent text and abstract-syntax-tree formats, maintaining all relationships & structure.
"Compose" means change of structure, so is not quite right. It suggests combining from separate independent parts (usually for the first time). Just as "decompose" suggests splitting into independent parts. They change form, not just format.
A quick search show's the term's used within:
Perl: http://perldoc.perl.org/B/Deparse.html
R: http://www.hep.by/gnu/r-patched/r-lang/R-lang_98.html
Common Lisp: http://www.clisp.org/impnotes/dffi.html#c-type-parse
PostgreSQL: http://doxygen.postgresql.org/deparse_8c.html
Eclipse: http://www.eclipse.org/forums/index.php/t/201883/
Unix Korn Shell: http://www.sourcecodebrowser.com/ksh/93tplus-p/deparse_8c.html