ICU-1134 make sure parse errors refer to the correct source line

X-SVN-Rev: 6698
This commit is contained in:
Alan Liu 2001-11-09 00:12:59 +00:00
parent dc74a1d2c0
commit 1b8e587611
5 changed files with 82 additions and 8 deletions

View file

@ -1177,7 +1177,8 @@ int32_t TransliteratorParser::syntaxError(UErrorCode parseErrorCode,
parseError.line = 0 ; /* we are not using line numbers */
// for pre-context
int32_t start = (pos <=U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1));
const int32_t LEN = U_PARSE_CONTEXT_LEN - 1;
int32_t start = uprv_max(pos - LEN, 0);
int32_t stop = pos;
rule.extract(start,stop-start,parseError.preContext);
@ -1185,9 +1186,8 @@ int32_t TransliteratorParser::syntaxError(UErrorCode parseErrorCode,
parseError.preContext[stop-start] = 0;
//for post-context
start = pos+1;
stop = ((pos+U_PARSE_CONTEXT_LEN)<= rule.length() )? (pos+(U_PARSE_CONTEXT_LEN-1)) :
rule.length();
start = pos;
stop = uprv_min(pos + LEN, rule.length());
rule.extract(start,stop-start,parseError.postContext);
//null terminate the buffer

View file

@ -134,6 +134,7 @@ TransliteratorTest::runIndexedTest(int32_t index, UBool exec,
TESTCASE(52,TestLocaleInstantiation);
TESTCASE(53,TestTitleAccents);
TESTCASE(54,TestLocaleResource);
TESTCASE(55,TestParseError);
default: name = ""; break;
}
}
@ -2600,6 +2601,30 @@ void TransliteratorTest::TestLocaleResource() {
}
}
/**
* Make sure parse errors reference the right line.
*/
void TransliteratorTest::TestParseError() {
const char* rule =
"a > b;\n"
"# more stuff\n"
"d << b;";
UErrorCode ec = U_ZERO_ERROR;
UParseError pe;
Transliterator *t = Transliterator::createFromRules("ID", rule, UTRANS_FORWARD, pe, ec);
if (U_FAILURE(ec)) {
UnicodeString err(pe.preContext);
err.append((UChar)124/*|*/).append(pe.postContext);
if (err.indexOf("d << b") >= 0) {
logln("Ok: " + err);
} else {
errln("FAIL: " + err);
}
return;
}
errln("FAIL: no syntax error");
}
//======================================================================
// icu4c ONLY
// These tests are not mirrored (yet) in icu4j at

View file

@ -256,6 +256,11 @@ class TransliteratorTest : public IntlTest {
*/
void TestLocaleResource(void);
/**
* Make sure parse errors reference the right line.
*/
void TestParseError();
//======================================================================
// Support methods
//======================================================================

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/translit/TransliteratorTest.java,v $
* $Date: 2001/11/05 20:25:23 $
* $Revision: 1.63 $
* $Date: 2001/11/09 00:11:01 $
* $Revision: 1.64 $
*
*****************************************************************************************
*/
@ -1935,6 +1935,28 @@ public class TransliteratorTest extends TestFmwk {
}
}
/**
* Make sure parse errors reference the right line.
*/
public void TestParseError() {
String rule =
"a > b;\n" +
"# more stuff\n" +
"d << b;";
try {
Transliterator t = Transliterator.createFromRules("ID", rule, Transliterator.FORWARD);
} catch (IllegalArgumentException e) {
String err = e.getMessage();
if (err.indexOf("d << b") >= 0) {
logln("Ok: " + err);
} else {
errln("FAIL: " + err);
}
return;
}
errln("FAIL: no syntax error");
}
//======================================================================
// icu4j ONLY
// These tests are not mirrored (yet) in icu4c at

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/translit/Attic/TransliteratorTest.java,v $
* $Date: 2001/11/05 20:25:23 $
* $Revision: 1.63 $
* $Date: 2001/11/09 00:11:01 $
* $Revision: 1.64 $
*
*****************************************************************************************
*/
@ -1935,6 +1935,28 @@ public class TransliteratorTest extends TestFmwk {
}
}
/**
* Make sure parse errors reference the right line.
*/
public void TestParseError() {
String rule =
"a > b;\n" +
"# more stuff\n" +
"d << b;";
try {
Transliterator t = Transliterator.createFromRules("ID", rule, Transliterator.FORWARD);
} catch (IllegalArgumentException e) {
String err = e.getMessage();
if (err.indexOf("d << b") >= 0) {
logln("Ok: " + err);
} else {
errln("FAIL: " + err);
}
return;
}
errln("FAIL: no syntax error");
}
//======================================================================
// icu4j ONLY
// These tests are not mirrored (yet) in icu4c at